Re: [mod_python] s ession lock

Jim Gallacher jpg at jgassociates.ca
Fri Apr 28 08:58:16 EDT 2006


yjfuk wrote:
> I think it's my code error,but I am not sure.
> My apapche conf is like this :
> SetHandler mod_python
> PythonHandler mod_python.publisher
>  
> I use psp as my template.I confuse that the variable seems to share the values among the pages.
> e.g. 
> a page named index.py contain functions as below:
>  
> test=0
> def a(req):
>     global test
>     req.write(str(test))
>     test=test+1
>  
> def b(req):
>     global test
> 
>     req.write(str(test))
>     test=test+1
>  
> when you visit the page http://localhost/index/a it print 0,then visit page http://localhost/index/b
> it print 1 as if the variable 'test' is pass vis get or post method.why?
> is it the cache trick? 

It's not a trick, it's the way python works. The interpreter created by 
mod_python is persistent between requests and you increment the value of 
test in your code.

However you can't depend on the value of test for apache-mpm's such as 
forked and worker. You'll have multiple interpreters with these mpm's 
and each interpreter will get it's own copy of the data. Which 
interpreter will handle a particular request so if you visit 
http://localhost/index/a 10 times you might see this seemingly random 
pattern:

0
1
0
2
1
3
0
1
4
3

Unless you fully understand this you should avoid variables defined at 
the module level for anything other than constants.

Jim


More information about the Mod_python mailing list