[mod_python] Mod_python & global persistence

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


Yes, I've found it much safer to pass the request object around
as a parameter:

    ddcServer.do_GET(req)

I wrap access to global objects with a thread lock, taking care
to ensure that the thread is unlocked even if an exception is
raised:

import thread
lock = thread.allocate_lock();
ddcServer = None

def handler(req):
    lock.acquire
    try:
        global ddcServer
        if not ddcServer:
            # create ddcServer ...
    finally:
        lock.release()
    ddcServer.do_GET(req)

--Alan

On Mon, 7 Mar 2005 17:04:32 -0500, "Graham Dumpleton"
<grahamd at dscpl.com.au> said:
> 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.


More information about the Mod_python mailing list