On Wed, Oct 5, 2011 at 5:36 AM, Sean DiZazzo <[email protected]> wrote:
> Hi,I have a Turbogears 2.1.2 app that kicks off a long running
> parallel task.  The task is run in many threads and on several
> machines,and they all need to use the model to update the database.
> What are the best practices for running DBSessions in threads?  I

You can try tgext.asyncjob it might do most of the work for you.
It will manage the session for you letting you just use the
model.DBSession inside the asynchronous threads.

About the session, by default TurboGears already provides a
sessionmaker which will create one scoped_session for each thread that
will try to use the DBSession object. The main issue is that TG
patches the session to use a different transactions manager which runs
inside the TG middleware stack by committing and reverting session
whenever is necessary. When you use the session outside of a request
anyway this middleware is not in place and so you will have to manage
the session yourself. By default tgext.asyncjob implements a worker
that behaves like TG, committing the session at the end of the async
function and reverting it in case of an exception.

Also pay attention that when you query an object from a different
thread it might not be available even thought you are sure that it
should be, to avoid this issue tgext.asyncjob provides a
asyncjob_timed_query that tries to retrieve an object from the
database multiple times until it excedes timeout.

-- 
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en.

Reply via email to