[mod_python] sharing data in the same interpreter

Miro mmadecki at comcast.net
Wed Apr 5 22:42:04 EDT 2006


Graham,
Thanks for the pointer.

I didn't realize that stock Apache on Linux/Unix uses forking MDM (seems a
bit conservative approach these days).

I had to recompile it with worker MDM that uses both threads and processes
(--with-mpm=worker) and configure it in such a way that it never uses more
than one process to serve requests (StartServers = 1, MaxClients =
ThreadsPerChild).

After that if I set up same PythonInterpreter to all handlers. It works with
virtual hosts and both http request and tcp connection handlers.

Just in case somebody runs into same problem.

Miro

> -----Original Message-----
> From: Graham Dumpleton [mailto:grahamd at dscpl.com.au]
> Sent: 04/03/2006 8:08 PM
> To: Miro
> Cc: mod_python at modpython.org
> Subject: Re: [mod_python] sharing data in the same interpreter
> 
> 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
> >
> 
> --
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.1.385 / Virus Database: 268.3.5/300 - Release Date: 04/03/06
> 

-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.385 / Virus Database: 268.3.5/301 - Release Date: 04/04/06
 




More information about the Mod_python mailing list