[mod_python] Mod_python and Sub-Interpreter segregation

Brian Hawthorne brianh at exelixis.com
Fri Sep 13 14:28:37 EST 2002

unfortunately, there is no way to share global data among multiple
apache child processes without using an external database of some sort.
this is true regardless of how many interpreters you have in each child
proc.  i anticipate that the multithreaded server model available in
apache2 will address this issue, though i haven't gotten my hands dirty
yet trying to set up modpy3 alpha with apache2.

brian hawthorne
Exelixis, Inc.

On Fri, 13 Sep 2002, 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
> -- 
> 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
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://www.modpython.org/mailman/listinfo/mod_python

More information about the Mod_python mailing list