[mod_python] sharing data in the same interpreter

Graham Dumpleton grahamd at dscpl.com.au
Mon Apr 3 20:07:43 EDT 2006


For startes read:

  http://www.dscpl.com.au/articles/modpython-004.html

This will explain how there are different process models in Apache
and a bit about data sharing.

Graham

 Miro wrote ..
> Hi,
> 
>  
> 
> I’m trying to figure out how to share data between different handlers
> running in the same interpreter instance.
> 
> I got it running with apache in debug mode (only one worker thread) but
> not
> in normal mode.
> 
> According to the value of “req.interpreter” both handlers use the same
> interpreter.
> 
> I noticed that with “PythonDebug On” according to the log message “handle”
> module gets reloaded with every request.
> 
> Does that wipe out any global cfg!?
> 
> Is there any way to make this working?
> 
>  
> 
> I’m using following platform:
> 
> -          Fedora Core 4
> 
> -          Apache 2.0.54
> 
> -          Mod_python 3.2.8 (installed from source to replace pre-packaged
> 3.1.4)
> 
>  
> 
> Complete cfg and example are attached.
> 
> test_handle.py does POST to set global data that follow on GET request
> should read.
> 
>  
> 
> Thanks
> 
> Miro
> 
>  
> 
>  
> 
> # /etc/httpd/conf.d/python.conf
> 
>  
> 
> LoadModule python_module modules/mod_python.so
> 
>  
> 
> Listen 8000
> 
> NameVirtualHost *:8000
> 
> <VirtualHost *:8000>
> 
>   SetHandler mod_python
> 
>   PythonPath "['/tmp/py'] + sys.path"
> 
>   PythonInterpreter ONE
> 
>   PythonAutoReload Off
> 
>   <Location "/get">
> 
>     PythonHandler handle::get_handler
> 
>   </Location>
> 
>   <Location "/post">
> 
>     PythonHandler handle::post_handler
> 
>   </Location>
> 
> </VirtualHost>
> 
>  
> 
> # /tmp/py/handle.py
> 
>  
> 
> from mod_python import apache
> 
>  
> 
> class SharedData:
> 
>   data = "Doesn't work"
> 
>  
> 
> def get_handler(req):
> 
>     req.content_type = "text/plain"
> 
>     req.write(SharedData.data)
> 
>     apache.log_error("get_handler wrote : " + SharedData.data)
> 
>     apache.log_error("get_handler interpreter : %r" % req.interpreter)
> 
>     return apache.OK
> 
>  
> 
> def post_handler(req):
> 
>     SharedData.data = req.read()
> 
>     apache.log_error("post_handler read : " + SharedData.data)
> 
>     apache.log_error("post_handler interpreter : %r" % req.interpreter)
> 
>     return apache.OK
> 
>  
> 
> # /tmp/py/test_handle.py
> 
>  
> 
> import httplib
> 
>  
> 
> def test_get():
> 
>    print "test_get"
> 
>    PORT = 8000
> 
>    PATH = "/get"
> 
>    conn = httplib.HTTPConnection("127.0.0.1:%s" % PORT)
> 
>    conn.putrequest("GET", PATH, skip_host=1)
> 
>    conn.putheader("Host", "%s:%s" % ("test_get", PORT))
> 
>    conn.endheaders()
> 
>    response = conn.getresponse()
> 
>    rsp = response.read()
> 
>    print "Got back : %s" %rsp
> 
>    conn.close()
> 
>  
> 
> def test_post():
> 
>   print 'test_post'
> 
>   params = 'It works'
> 
>   print "    writing %d bytes..." % len(params)
> 
>   PORT = 8000
> 
>   PATH = "/post"
> 
>   conn = httplib.HTTPConnection("127.0.0.1:%s" % PORT)
> 
>   conn.putrequest("POST", PATH, skip_host=1)
> 
>   conn.putheader("Host", "test_rpost:%s" % PORT)
> 
>   conn.putheader("Content-Length", len(params))
> 
>   conn.endheaders()
> 
>   conn.send(params)
> 
>   conn.close()
> 
>  
> 
> if __name__ == "__main__":
> 
>   test_post()
> 
>   test_get()
> 
> 
> -- 
> No virus found in this outgoing message.
> Checked by AVG Free Edition.
> Version: 7.1.385 / Virus Database: 268.3.5/300 - Release Date: 04/03/06
>  


More information about the Mod_python mailing list