[mod_python] Does mod_python have a global dictionary or sorts?

Byron Ellacott bje at apnic.net
Tue Jul 6 09:43:36 EDT 2004

James Gate wrote:
> I've started writing Web Applications using mod_python and was wondering 
> if there is some sort of global dictionary that I can register objects 
> in, similar in method to that of Java Servlets? What I'm trying to do is 
> create a global connection pool for database access, that is accessible 
> by all apache threads. I've written and tests a thread safe connection 
> pool, and just need somewhere to place an instance of it!

(As long as you're running a threaded Apache, and not a forking Apache...)

Just stick it in a global variable.  You can make it a singleton 
instance easily enough:

--- snip DBPool.py ---
#!/usr/bin/env python

class DBPool:

pool = DBPool()
--- snip DBPool.py ---

mod_python reuses Python interpreters, so once your module has been 
loaded, any subsequent imports of it will just be obtaining a reference 
from sys.modules[].  You'll want to watch for how you've told mod_python 
to create interpreters; by default it creates one interpreter per 
process, but you can also tell it to create an interpreter per directory 
or a specifically named interpreter.


More information about the Mod_python mailing list