I've been reading through the SQLAlchemy docs for a while now and am still 
fairly confused about using SQLAlchemy with threaded processes.  Hopefully 
my specific question helps me understand things a little more...

I'm using the Pyramid framework with SQLAlchemy.  The default scaffold for 
this setup has the following line:

    DBSession = 
scoped_session(sessionmaker(extension=ZopeTransactionExtension()))

I understand this essentially sets things up so in my web application I can 
just do `DBSession.query(...)` and it automatically manages sessions for me 
based on each web request.  There's also some sort of interaction with this 
and the `pyramid_tm` transaction manager.  This is all fine for my regular 
web application.

My problem comes up when I'm trying to create a portion of my application 
which spawns several threads to listen for other tcp connections and then 
save results in the database.  Right now that portion of the application 
maintains a 20-thread pool to handle incoming connections.  I'm passing the 
DBSession into the function that spawns the threads and then it's used as a 
global variable similar to how the rest of the web application uses it.  I 
wanted to find out if this is the proper way of handling this.

I think since the sessions are thread-local there isn't any session sharing 
across threads.  However, I'm not sure how the "scoped_session()" works in 
these long-running threads.  I'm a little concerned that it's opening a 
transaction and then sitting on it without ever committing...  I'm really 
not sure.

Should I change it so my threads instead use something like:

   with transaction.manager:
       DBSession.add(SomeORMObject(id=4))

or:

   with DBSession.begin():
       DBSession.add(SomeORMObject(id=4))

Will that commit the transaction and then release the session connection at 
the end of the context?

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to