[mod_python] Thread safety

mike bayer mike_mp at zzzcomputing.com
Mon Jun 6 17:05:43 EDT 2005

if you are running Apache 2.0 with the "worker" MPM, then you need to be
threadsafe.  if you are running "prefork" MPM or Apache 1.3, it only uses
child processes for concurrent operation and is not threaded.  of course
it might be a good idea to be threadsafe no matter what for portability

Python 2.4 has a threadlocal object via threading.local() which I do in
2.3 like this:

class ThreadLocal(object):
    def __init__(self):
        object.__setattr__(self, 'tdict', {})

    def __getattribute__(self, key):
            return object.__getattribute__(self, 'tdict')["%d_%s" %
(thread.get_ident(), key)]
        except KeyError:
            raise AttributeError(key)

    def __setattr__(self, key, value):
        object.__getattribute__(self, 'tdict')["%d_%s" %
(thread.get_ident(), key)] = value

> When using a request handler, mod_python calls handler(req) for every
> incoming request. I would say it's a safe guess that this implies that
> handler can be called concurrently in different threads and anything
> done in this function and any functions it calls must be thread safe.
> I didn't find any mention of this in the docs (I didn't look too
> hard), so I thought I'd verify with the people sure to know.
> Thanks,
> -Dan
> _______________________________________________
> 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