[mod_python] Mod_python & global persistence

Graham Dumpleton grahamd at dscpl.com.au
Mon Mar 7 17:04:32 EST 2005


The variable assignments and code at global scope within one instance of an
interpreter should not be getting executed on each request. What you are
possibly getting confused other is that if running in prefork mode for Apache,
there will be multiple processes, each with their own interpreter and thus their
own copy of the load module and data.

Thus, the data assignment and code at global scope will be executed once
per process. Each process has its own copy. The data is not shared across
processes. You will need to use some form of external persistence if you
want data shared between processes.

Just to make things more confusing, if using a multithreaded MPM for Apache,
there are some bugs in mod_python which can cause multiple interpreters to
be incorrectly created within one process. Further within one interpreter, a
module at startup may be loaded more than once, thus causing code to be
executed more than once. Fixes for the bugs can be found at:

  http://www.dscpl.com.au/projects/vampire/PATCHES

BTW, it is not generally good practice to be caching a request object into
global data structures as the request object only really exists in a valid
state for the length of the request. Also, in general the approach you are
using looks like it would fail in a multithreaded MPM. This is because more
than one request could invoke the handler at the same time, and thus they
will muck up each other because they both manipulate the global data
without any exclusion locks.

Wagner,Harry wrote ..
> 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