简体   繁体   中英

SQLAlchemy Concurrency and Queries

I have a number of separate machines, each of which uses a separate instantiation of

Session = scoped_session(sessionmaker(bind=engine)) .

If machine A runs the code

session = Session()
session.add(Foo(pk=1))
session.commit()

what do I have to do ensure that when machine B runs the below code...

session = Session()
result = session.query(Foo).get(1)

... that result holds the new row of Foo that machine A created (assume that machine A's code ran first). I've had problems before where Session "A" commits an object but another Session "B" cannot find it in a query until I reinstantiate Session "B".

scoped_session wraps Session in a thread local storage object; if you're doing more work to handle threads, you might want to either refactor your code to use a single scoped_session instance for all threads, or use the result of sessionmaker directly, without wrapping it, again one for all threads, and call Session() for each thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM