[mod_python] Using threading.local with Worker MPM

Graham Dumpleton grahamd at dscpl.com.au
Wed Aug 16 18:40:09 EDT 2006

Gavin Panella wrote ..
> I was using a PythonInitHandler to set up various objects for the rest
> of my 
> request. I set up a shared 'space' object which is based on threading.local
> because this code needs to run in several different scenarios, not just
> under 
> mod_python, or even in a web context.

Just setting data as attributes of the mod_python request object is generally
preferred, but if all code doesn't get passed the request object, then what
you have done is probably reasonable.

> However, any object I put into 
> the 'space' in the PythonInitHandler was disappearing by the time the main
> PythonHandler started.

I presumed you added calls to apache.log_error() to confirm your inithandler()
was actually being called?

Anyway, first off is that there are bugs in mod_python 3.1.4 related to the
creation of the Python interpreters and thread locking. I am not sure how
this would affect use of thread local storage, perhaps not depending on
where the data is held, but would definitely suggest you upgrade to 3.2.10.
This doesn't eliminate all module reloading problems, but gets rid of a few
nasty ones.

Second is that your success may be determined by what directives you are
using related to the Python interpreters. If you are using either of the directives
PythonInterpPerDirective or PythonInterPerDirectory then you can have
problems. Some of these are related to bugs in 3.1.4, some of which are
fixed in 3.2.10.

The basic problem with interpreters here is that if you are using these
directives, or even if you are using PythonInterpreter directive explicitly, is
that the inithandler() could be executed within the context of a different
interpreter to your handler(). To check, out messages to the Apache error
log in both which show the value of req.interpreter.

If they are different post the results back to the mailing list along with how
you are using the above directives, showing what the enclosing Directory
and or Location directives are that enclose your handler directives.

> It took hours to track down this behaviour, and today I've also been having
> lots of fun with PythonAuthzHandler (which I eventually abandoned) so it's
> been a very frustrating and non-productive kind of day :-) Please, someone
> help me!

You need to know the correct set of directives to use in order to get the
authhandler() and authzhandler() to trigger. This includes use of the Require
directive but possible also AuthType and AuthName. Problem is that in 3.1.4
even if you can get it to trigger you still may have problems if you have
multiple stacked auth[z]?handler() functions as mod_python doesn't interpreter
return status codes properly. This is fixed in 3.3.

Suggest you post to the mailing list what directives you were using to try and
attempt to trigger the authzhandler().


More information about the Mod_python mailing list