Here's how I've addressed the same problem on a per-transaction basis,
based on some ideas of Valentino Volonghi (aka Dialtone):

http://foss.eepatents.com/pNetworkX/browser/trunk/pnetworkx/database.py

Note particularly the "transact" function, which is used as a decorator.

Here's a usage example, with various database-accessing methods that are
decorated with "@transact":

http://foss.eepatents.com/pNetworkX/browser/trunk/pnetworkx/edges.py

Best regards, Ed

Dimi Shahbaz wrote:
> All,
> 
> Here is the pattern I have adopted for putting long-running queries 
> into a thread, to avoid blocking the entire application.  I'm using 
> twisted's deferToThread().
> 
> def runInThread(fun, *args, **kwargs):
>   opts = schema.default_engine.engine.opts
>   dbname = schema.default_engine.engine.name
>   echo = schema.default_engine.engine.echo
>   d = threads.deferToThread(_runInThread, dbname, opts, echo, fun, 
> *args, **kwargs)
>   return d
> 
> def _runInThread(dbname, opts, echo, fun, *args, **kwargs):
>   global_connect(dbname, opts, echo=echo)
>   return fun(*args, **kwargs)
> 
> ....
> 
> d = runInThread(Person.mapper.select)
> d = runInThread(Person.mapper.select_by, person.c.age >= 21)
> 
> 
> Basically, runInThread runs in the main thread context.  It pulls the 
> db connection parameters out and passes them to _runInThread (which 
> runs in a thread).  I'm doing this because as I understand it, each 
> thread has it's own connection.  Of course, I get deferred objects  back
> from runInThread, so the surrounding code has to deal with that...
> 
> I just want to make sure my pattern above is sound.  Is there an  easier
> way to handle multiple threads connection to the same db, or  something
> I've overlooked?
> 
> 
> -- 
> Dimi Shahbaz, Software Engineer Specialist
> California PASS Program
> www.cyberhigh.fcoe.k12.ca.us


-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to