[Twisted-Python] Newbie - how to modify my db-intensive app to use Twisted

Clark C. Evans cce at clarkevans.com
Sun Nov 20 12:04:21 EST 2005


Frank,

There are two approaches depending upon your database driver.  If you
have a driver that supports async queries (such as psycopg2) then it
won't block while waiting for the query results.  In this case, your
"execute query" just returns a Deferred; and when the driver is notified
that the results are available, the deferred's callback is executed.

The second approach, usable if you don't have an async driver (like
pyPgSQL) is to use runInteraction.  Basically, in this approach you
don't really get the advantages of Twisted's core event loop; since
you put most of your request handling code in the runInteraction,
which returns a Deferred when it is completed.   This is the approach
my current code uses; but back when I wrote it psycopg2 didn't exist.

The primary advantage in the latter case, over a multi-threaded server,
such as Webware (for example), is that you have access to all of the
wonderful protocol handlers and, IMHO, a stellar development team.
This latter point -- the people -- is the driving reason for me to
continue to use Twisted (as my work is also heavily database intensive).

Cheers,

Clark





More information about the Twisted-Python mailing list