[mod_python] Initial configuration and persistence

Jim Gallacher jpg at jgassociates.ca
Mon Jun 5 13:55:03 EDT 2006

Richard Lewis wrote:
> Hi there,
> I'm new to mod_python and have nearly finished writing a module implementing a 
> subset of Apache Cocoon's functionaiity, providing URI pattern matching, 
> Berkeley DB XML-powered XQuery and libxslt-powered XSLT transformations.
> There are a couple of things I'm stuck on, though.
> My handler works fine when tested from the command line and it also works from 
> Apache when I include my special XML configuration file directly in the 
> handler source code. However, I'd like to be able to have the configuration 
> file separate from the handler source code so that I can use the handler 
> unaltered for multiple sites.
> What I can't do is work out how to pass the file name of the XML configuration 
> file as a parameter to mod_python from my Apache configuration file when my 
> handler is initially loaded. I've added an Apache SetEnv directive and I can 
> see it from apache.config_tree() but I can't work out how to get its value.
> My VirtualHost configuration currently looks like this:
> NameVirtualHost *
> <VirtualHost *>
>     ServerName www.studios.uea.ac.uk
>     DocumentRoot /var/www-studio
>     SetHandler mod_python
>     PythonDebug On
>     <Directory /var/www-studio>
>         SetEnv sitemap_config sitemap.xml
>         PythonHandler sitemap
>     </Directory>
> </VirtualHost>
> Any ideas how to get that SetEnv's value from inside sitemap.py?

     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.

> The other thing I'm stuck on is implementing persistence. I currently have my 
> DB XML manager, some compiled XSLT stylesheets and other configuration data 
> stored as global variables in my handler. However, from my Apache log, it 
> looks as though it reloads my handler module every time a request is made, 
> thus reloading the database connection, re-compiling the stylesheets etc. 
> when I'd rather they just persisted between requests. Is there a way of 
> making them persist? Or is it just my imagination that they are being 
> reloaded?

You can turn off reloading with "PythonAutoReload Off".

Persistence is tricky, depending on which apache-mpm you are using. For 
mpm-worker or mpm-prefork you'll have multiple processes, each with it's 
own interpreter. You can cache data at the module level, but recognize 
that you may have multiple copies of that data - changes in the data in 
one interpreter will not be reflected in another.

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.


More information about the Mod_python mailing list