[mod_python] Mod_python and Sub-Interpreter segregation

Jeff Davis list-mod_python at empires.org
Fri Sep 13 14:48:57 EST 2002


Hi,

The only way I know of that an interpreter can be shared is if you have a 
multithreaded app.

I think that's possible because it seems to me that python does well with 
threads. That way, each request could execute in it's own interpreter 
function call, and that way they'd be separate, but allow you to share global 
variables. However, that seems like it might be a little tricky, so I am not 
sure how feasible it is. Also note that the data wouldn't survive an apache 
restart.

However, to have the one-process-many-threads per virtualhost in apache2, you 
need to use mpm perchild. That mpm isn't really stable, so you might need to 
wait a while.

You might consider a lightweigtht DB or a RAM based filesystem or something in 
the interim. Or, if you wanted a featureful database you could use postgresql 
and persistant connections and it would still operate at a pretty good speed, 
which could be a good idea if you need to use a database for your application 
anyway.

Regards,
	Jeff

On Friday 13 September 2002 10:11 am, Bryan Mongeau wrote:
> Greetings,
>
> I've been playing around with mod_python and have noticed a peculiar
> behaviour that perhaps someone could explain to me.  As the mod_python
> documentation explains, a sub-interpreter is created for each virtual host
> (default).  I therefore expected global variables within this
> sub-interpreter to be able to maintain state between hits.  I created a
> simple script ( included below ) to observe this behaviour by counting hits
> in a global variable and printing the interpreter name.
>
> To my surprise, it seems as if different apache child processes run their
> python code in different sub-interpreters even though the interpreter name
> remains the same ( virtualhost ). I produced this behaviour by loading the
> script in my browser and refreshing (very) rapidly. What happens is the hit
> counter eventually returns to zero, meaning that the global variable is no
> longer in the namespace. Repeated refreshing causes the hit counter to jump
> all over the place as I presume it jumps from one apache child process to
> another, each having their own sub-interpreters and global variables.
>
> It seems as if each child process has its own sub-interpreter? Or am I
> confused? Was this documented? Enlightenment is greatly appreciated.
> Unfortunately the behaviour I am observing is undesired.
>
> Here is the code to demonstrate:
>
> from mod_python import apache
> def handler(req):
>     try:
>         globals()['hits']+=1
>     except KeyError:
>         globals()['hits'] = 0
>     req.write("interpreter_name=" + req.interpreter )
>     req.write(" - hits: " + str(globals()['hits']))
>     return apache.OK




More information about the Mod_python mailing list