[Twisted-Python] Start-up code and passing its results

Dave Kuhlman dkuhlman at cutter.rexx.com
Fri Jun 13 20:05:36 EDT 2003

Here is a seemingly simple question.  But after searching the docs,
I still haven't figure it out.

How do I write some start-up code for a Web app, then pass values
from the start-up code to objects that respond to requests?  In
particular, how do I create a DB ConnectionPool, then pass that to
the implementation of a Web page?

If the above question made sense, ignore the following.  It's more
struggling with the same problem.

I'm learning Twisted.  I've made quite a bit of progress, although
I'm slower than most.  I've been able to build a small sample Web
application.  I like what I find in Twisted so far.

After some digging, I've been able to use
enterprise.adbapi.ConnectionPool in one of my Web pages.  But,
when I look in my log file, I see the messages "Connecting to
database: pyPgSQL.PgSQL" and "adbapi connecting: pyPgSQL.PgSQL"
for *every* time I visit that Web page.  That makes me think that
I'm creating a new connection every time I deliver that Web page,
which, of course, a connection pool should eliminate.  Am I right?

So, what am I doing wrong?  I'm guessing that I should not be
running the following statement every time I deliver this Web page:

    dbpool = adbapi.ConnectionPool("pyPgSQL.PgSQL",

So, my question is: Where do I put the above statement?  And, how
do I share the same dbpool across delivery of that Web page and
across all sessions in my Web application?

By the way, I created my Twisted application with the following:

  mktap web --path Test1 --processor=.rtl=twisted.web.script.ResourceTemplate

And, I start up my server with the fillowing:

  twistd -f web.tap

Am I supposed to run mktap with the "--type=source" option, and
then create the connection pool in the resulting .tas file?  If so:

  1. How do I add code to the .tas file?  How do I get it called
    during start-up?

  2. How do I pass the dbpool (or any variable) to the Web page?

Another way of asking this question is: How do I execute code at
server start-up?  And, how do I make the results of that start-up
code available to the various parts of my application, in
particular to a Python module that implements the response to a

Or, am I trying to do things the non-Twisted way?  If so, how do I
learn the Twisted way?

  - Dave

Dave Kuhlman
dkuhlman at rexx.com

More information about the Twisted-Python mailing list