Simple example of Session usage in multi threaded context

682 views
Skip to first unread message

Venkatesh

unread,
Jun 6, 2008, 3:14:53 PM6/6/08
to sqlalchemy
Hello,
I'm trying to use SQLAlchemy for my Python project which is a multi-
threaded application. While I've had good success in performing CRUD
operations with a single thread, I'm having a lot of trouble when I
start using multiple threads.

Can anybody give a simple example of using the Session in a multi
threaded context? I'm looking for a simple example like this:

class User(object):
- this is the User class which has the users_table

class ThreadOne
- open a thread local session
- perform operations on user objects
- save the user object

class ThreadTwo
- open another thread local session
- connect to the same db, and get user objects
- modify the user objects
- save this to the db

Please help.
thanks,
Venkatesh.

Michael Bayer

unread,
Jun 6, 2008, 9:29:04 PM6/6/08
to sqlal...@googlegroups.com


use one session per thread, share nothing between threads. The
scoped_session should make this pretty straightforward.

http://www.sqlalchemy.org/docs/04/session.html#unitofwork_contextual

Reply all
Reply to author
Forward
0 new messages