[mod_python] sharing data in the same interpreter

Miro mmadecki at comcast.net
Mon Apr 3 19:52:10 EDT 2006



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

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


Complete cfg and example are attached.

test_handle.py does POST to set global data that follow on GET request
should read.






# /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 "/post">

    PythonHandler handle::post_handler




# /tmp/py/handle.py


from mod_python import apache


class SharedData:

  data = "Doesn't work"


def get_handler(req):

    req.content_type = "text/plain"


    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("" % PORT)

   conn.putrequest("GET", PATH, skip_host=1)

   conn.putheader("Host", "%s:%s" % ("test_get", PORT))


   response = conn.getresponse()

   rsp = response.read()

   print "Got back : %s" %rsp



def test_post():

  print 'test_post'

  params = 'It works'

  print "    writing %d bytes..." % len(params)

  PORT = 8000

  PATH = "/post"

  conn = httplib.HTTPConnection("" % PORT)

  conn.putrequest("POST", PATH, skip_host=1)

  conn.putheader("Host", "test_rpost:%s" % PORT)

  conn.putheader("Content-Length", len(params))





if __name__ == "__main__":



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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mm_cfg_has_not_been_edited_to_set_host_domains/pipermail/mod_python/attachments/20060403/c7862eae/attachment.html

More information about the Mod_python mailing list