[mod_python] Mod_python & global persistence

Wagner,Harry wagnerh at oclc.org
Tue Mar 15 15:43:43 EST 2005


Alan, Graham,
Thanks for your help with this, and sorry for the delay in responding. I
am still trying to understand what is happening here.  I changed my test
script to: 

from mod_python import apache
import sys, urllib, urlparse, xml.dom.pulldom, cgi, socket,
xml.sax.saxutils, StringIO, math, glob, os.path, time

ddcServer = None
resp = None

def list(req):
	global ddcServer
	now = str(time.time())
	if ddcServer:
		ddcServer.testing = "ddcServer found"
	else:
		ddcServer = DDCServer("ddcServer not found")
	resp = '''
	<?xml version="1.0" encoding="iso-8859-1"?>
	<!DOCTYPE html\n
	  PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"\n
	  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n
	<html xmlns="http://www.w3.org/1999/xhtml">\n
	  <head>\n
	    <title>ddcServerT</title>\n
	  <body>\n
	  <h1>''' + ddcServer.testing + '''</h1>
	  <h2>''' + now + '''</h2>
	  </body>
	</html>\n
	'''
	return resp
	
class DDCServer(object):
    def __init__(self, testing):
	self.testing = testing

And wrapped it in another small bit of code (ddcServerTest.py):

from mod_python import apache
from ddcServerT2 import list
import sys, urllib, urlparse, xml.dom.pulldom, cgi, socket,
xml.sax.saxutils, StringIO, math, glob, os.path

def handle(req):
	return list(req)

This is the code that gets invoked by the mod_python.publisher handler.
The key was to only import the list function and not the ddcServerT2
module. 

I can't explain the results, but the first few get requests to
ddcServerTest.py produce 'ddcServer not found'.  After that the global
remains resident in memory (I get ddcServer found).  The display time
changes so I know it is not being cached by my browser.  

Is this reliable?  Any ideas why it takes a few get requests before the
global remains resident?

Thanks!  harry 


---
not sure why your code wouldn't work. I use the equivalent of the
following to get the effect you're looking for:


ddcServer = None

def handler(req):
    global ddcServer 
    if not ddcServer :
        ddcServer = DDCServer()
        # ...
    # ...


Incidentally, I once had problems with huge memory leaks when I wrote
code similar to yours that assigned req to a variable that persisted
across calls. They were fixed by assigning the variable to another value
before returning (strange but true):

    ddcServer.req = req
    ddcServer.do_GET()
    ddcServer.req = None

--Alan


On Mon, 7 Mar 2005 16:32:27 -0500, "Wagner,Harry" <wagnerh at oclc.org>
said:
> Is there a way with mod_python to init global variables in a handler 
> the first time it is called, so that they are available (and already
> initialized) in subsequent calls to the handler?  I thought the 
> following might work, but no joy:
> 
> from mod_python import apache, util, Session
> 
> def handler(req):
> #       req.log_error("in handler")
> 	global ddcServer
> 	ddcServer.req = req
> 	ddcServer.do_GET()
> 	return apache.OK
> ...
> ddcServer = ''
> if not ddcServer:
> 	...
> 	ddcServer = DDCServer(None)	
> 	ddcServer.captions = DDCCaptions(captionsFile)
> 	ddcServer.localText = TextLocalization(captionsFile)
> 	ddcServer.ddcSearch = ddc3.DDCSearch(oclc2ddcFile, recFileName,
> compFile)
> 
> The 'if not ddcServer' code is invoked with every call to the handler.
> Any ideas what I am doing wrong, or if this is possible with
mod_python?
> 
> 
> TIA... harry
> 
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python



More information about the Mod_python mailing list