[mod_python] Singleton Object

Graham Dumpleton graham.dumpleton at gmail.com
Thu Dec 13 03:13:04 EST 2007

On 13/12/2007, Tom Wells <drshade at gmail.com> wrote:
> Hi everyone,
> I've been evaluating mod_python to implement a particular set of
> requirements, and am now convinced that it is going to work for me, however
> I have one remaining problem to solve:
> I need to be able to spawn a single "watchdog" thread within my module which
> connects back to a central server (via webservice calls) to provide
> monitoring data, plus synchronize certain session data. I need to support
> apache2.2 running in worker mpm (on linux platform), as well as winnt. In
> the winnt deployment it's easy to spawn a single thread, as i can do this
> the first time my module imports, but in worker mpm my module is imported
> for each apache process started (I haven't actually tested the winnt side so
> it's an assumption after some heavy reading up on the apache threading
> models).
> After some research with the C apache api I've noticed a hook called
> "ap_hook_post_config" which seems like a good place to spawn singleton
> objects, but I couldn't track this down in the mod_python hooks, nor have I
> actually tried it with the C api, so I could be very wrong!
> I've also taken a look at the mod_python Session bits, hoping to be able to
> possibly set a global flag somewhere so that further module imports don't
> spawn more threads, but I need something which is non-persistent (so if
> apache restarts that cache is cleared, which I get from the MemorySession,
> except I can't share this across processes, catch-22).
> Anyways, any suggestions would be greatly appreciated!

If you expect state to be available in each Apache child process when
run on UNIX, you cant avoid having multiple distinct connections back
to the back end system to collect the data. Playing with the C API
post config hook isn't going to help you.

If a connection from each Apache child process is okay, then use
PythonImport directive to import a module on process startup which
does any once of initialisation.


More information about the Mod_python mailing list