[mod_python] Quick question from mod_perl user

Graham Dumpleton grahamd at dscpl.com.au
Sat Jan 22 21:35:32 EST 2005


On 23/01/2005, at 7:03 AM, snacktime wrote:

> I have an application that I need to write in mod_python, and I was
> curious if global variables persist like they do in mod_perl, or is
> that a non issue with mod_python?

I am not familiar with mod_perl, and thus not totally clear on what you
mean by "persist" in this particular case, but a global variable within
a module will in mod_python persist across distinct requests.

This global though is only accessible to that specific Python 
interpreter,
thus if using prefork mode of Apache, different processes obviously have
different versions of that global. If in one process, distinct 
interpreters
are created, again each interpreter, each has their own version of that
global.

Obviously, a global does not persist across restarts.

Note that if using a multithreaded MPM with Apache, the global will need
to be adequately threaded protected if access is more than read only.

There are also various issues that need to be dealt with if you enable
module reloading. This is because a global which is setup during module
import will be replaced when a module is reloaded unless you add extra
code to detect when a module reload is occurring and preserve the 
existing
copy of the global data, rather than replacing it.

I have rambled on about issues with globals, module reloading and 
threading
previous on the mailing list. One of these threads was:

   http://www.modpython.org/pipermail/mod_python/2004-October/016605.html

There were possibly other threads as well, so you should search the
mailing list archive on the mod_python web site.

> Also, can I set the handler to use a .pyc file instead of the source
> file (.py) ?  Such as:
>
> AddHandler mod_python .pyc

The extension for AddHandler isn't referring to the name of the Python 
code
file, but the extension which appears in the request URL. Thus, if you 
have:

   AddHandler mod_python .py
   PythonHandler mymodule

what it means is that if a request URL ends in ".py" extension, then 
your
Python module called "mymodule" will be used.

Because the basic import module mechanism used by mod_python is the 
same as
is used in normal Python imports, it will pick .py, .pyc or even .so 
variants
of modules. Thus, for a .py, if it has already been compiled into .pyc, 
it
will used the compiled module. If the user/group that Apache runs as is 
such
that it has permission to write a file into directory where .py exists, 
if
no .pyc file exists, it will write the compiled file automatically, and 
next
time the module is required, for example after a restart, the compiled 
module
will be used instead.

Graham



More information about the Mod_python mailing list