[mod_python] Mod_python & global persistence

Alan Davies alan at goatpunch.com
Mon Mar 7 17:17:50 EST 2005


Hi,

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