[mod_python] Mod_python and Sub-Interpreter segregation

Bryan Mongeau bryan at eevolved.com
Fri Sep 13 17:11:22 EST 2002


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
-- 
Bryan Mongeau
eEvolved Inc. - IT Consulting & Custom Software
http://eevolved.com/
--
"The true function of life, that which is being maximized in the natural 
world, is DNA survival. But DNA is not floating free; it is locked up in 
living bodies and it has made the most of the levers of power at its 
disposal." -- Richard Dawkins




More information about the Mod_python mailing list