[mod_python] Initial configuration and persistence

Richard Lewis richardlewis at fastmail.co.uk
Tue Jun 6 06:29:28 EDT 2006

On Monday 05 June 2006 18:55, Jim Gallacher wrote:
> Richard Lewis wrote:
> >
> > Any ideas how to get that SetEnv's value from inside sitemap.py?
> Use
>      req.add_common_vars()
>      env_dict = req.subprocess_env
> Note that the keys in subprocess_env will be pre-pended with REDIRECT_
> if you do an internal redirect.
> Another option would be to use PythonOption.
>      PythonOption yournamespace.config /path/to/config
> and retrieve with
>     option_dict = req.get_options()
> Note that both of these are only usable when the request is being
> processed, not when the module is being imported. You could have your
> handler check if everything is initialized before proceeding. A more
> transparent way would be to do this check in an earlier phase such as
> PythonFixupHandler. That way it will be transparent to your handler.
Thanks for this. I've added some to code to the beginning of my request 
handler which checks if the global persistant variables are empty and, if 
they are, it retrieves the name of the configuration file from the SetEnv 
directive using req.subprocess_env as you suggested and initialises the 
handler correctly.

> > The other thing I'm stuck on is implementing persistence.
> Modules loaded with the standard python import statement will not be
> reloaded by mod_python. That combined with the fact that each
> interpreter gets it's own namespace may open some other options for you.
> Something like this may work, but it is totally untested:
> For each VirtualHost (n) in your apache config:
> PythonPath "['/path/to/site/virtualhost-n'] + sys.path"
> Each virtualhost gets its own siteconfig.py, but in directory virutalhost-n
> In your handler module use:
> import siteconfig
> Hopefully (I did say untested, right?) each interpreter will import the
> correct siteconfig module. You could the use that same module for
> caching bits of data. The drawback is that you'll need to restart apache
> when you make any changes to siteconfig.py
> Either way, you'll need to make sure you are accessing your module data
> in a thread-safe manner for mpm-worker.
I have yet to test using my code on the main server which provides all the 
VirtualHosts I intend to use, but I'll keep this in mind in case I can't get 
it to work.

On Monday 05 June 2006 18:45, Colin Bean wrote:
> Hi Richard,
> Here's a couple of things to address your second question.  If you're
> seeing messages in your error logs like "(re)importing module *your
> handler*", this doesn't necessarily mean that the module was reloaded
> and your global persistence was lost; it's a diagnostic message
> generated when you've enabled PythonDebug.
> One method that I've found useful for determining when global code is
> executed is to add a print statement in the global scope right after
> setting up your persistent stuff (to print to the error log without
> having a request object available, you can do something like this:
> http://www.modpython.org/FAQ/faqw.py?req=edit&file=faq02.003.htp ).
> This will give you a much better idea of when your modules are being
> reloaded.

Yes, that is the message I was seeing. And I tested it further: if I make lots 
of requests to my handler, I only get a handful of "(re)importing module" 
entries, so that must be because of the multiple instances of Apache as you 
said. I also tried making changes to my XSLT stylesheet source files as a 
test. These are compiled when the module is loaded and stored as global 
variables. However, when I changed the stylesheet sources, the handler's 
response did not reflect the changes until I restarted Apache which means 
that they must be persisting correctly across requests.

Thanks for your help.

BTW, my set up is apache2.0.55-mpm-prefork, mod_python 3.2.8 both a supplied 
in Debian.


Richard Lewis
Sonic Arts Research Archive

More information about the Mod_python mailing list