[mod_python] Connection Pools

j a inboundfilter at gmail.com
Wed Jan 16 15:14:11 EST 2008


The topic has been covered many times before, but I'm hoping someone
can clarify what to expect when using the technique described in this
thread: http://www.modpython.org/pipermail/mod_python/2005-October/019287.html

Specifically:
> Thus, it is preferable that resource allocation and the storage of the
> handle be done in a Python module that is not imported using the module
> loading system of mod_python but that "import" be used instead. Such a
> module should preferably not be in the document tree but elsewhere on
> the Python path. As described in the link above, use an access function
> in that module which returns, and creates as necessary, the resource
> handle.

That's basically what I'm doing, under Apache 2.2 with mpm_worker,
mod_python 3.3, Publisher handler, postgres, psycopg2. In my
implementation, the module has one top-level name for a
psycopg2.PersistentConnectionPool object, an init_pool() function to
set that object, a get_connection() function which returns a reference
to it, and a release(conn) function to return the connection to the
pool. Additionally, get_connection() will call init_pool() if the pool
object is None. The applications use only the get_connection() and
release(conn) functions. As far as I can tell, this is working fine.

What I'm unsure of:
1) How many pools could I potentially end up with? One pool per httpd
process, or per interpreter?

2) Are those pools and their connections available to all the threads
in a given process?

3) Will imports in the applications overwrite the pool object
initialized by prior calls to init_pool() or get_connection()? Related
to #2? Does module caching deal with this?

4) Will the pool(s)/connections be closed when Apache kills off a
process? Automatically? Will I need a specific cleanup function?
How/when would that be called()?

I've also read this whole thread:
http://www.modpython.org/pipermail/mod_python/2005-September/019092.html,
and this article: http://www.dscpl.com.au/articles/modpython-004.html,
both of which were helpful with how to deal with the connections, but
don't really talk about what I'm asking here.


More information about the Mod_python mailing list