On 23/06/14 19:05, smur...@gmail.com wrote:
On Monday, June 23, 2014 5:54:38 PM UTC+2, Lie Ryan wrote:

If you don't want each thread to have their own copy of the object,

Don't use thread-scoped session. Use explicit scope instead.

How would that work when multiple threads traverse the in-memory object 
structure and cause relationships to be loaded?
IIRC sqlalchemy's sessions are not thread safe.

You're going to have that problem anyway, if it is as you said that your problem is that you don't want each thread to have their own copy, then you cannot avoid having to deal with concurrent access. Note that SQLAlchemy objects can be used from multiple thread as long as it's not used concurrently and the underlying DBAPI is thread-safe (not all DBAPI supported by SQLAlchemy are thread safe). You can detach/expunge an SQLAlchemy object from the session to avoid unexpected loading of relationships.

Alternatively, if you are not tied to SQLAlchemy nor SQL-based database, then you might want to check out ZODB's ZEO (http://www.zodb.org/en/latest/documentation/guide/zeo.html):

> ZEO, Zope Enterprise Objects, extends the ZODB machinery to
> provide access to objects over a network. ... ClientStorage
> aggressively caches objects locally, so in order to avoid
> using stale data the ZEO server sends an invalidation message
> to all the connected ClientStorage instances on every write
> operation. ...  As a result, reads from the database are
> far more frequent than writes, and ZEO is therefore better
> suited for read-intensive applications.

Warning: I had never used ZODB nor ZEO personally.


Reply via email to