[mod_python] Using threading.local with Worker MPM

Gavin Panella gavin at premolo.com
Wed Aug 16 16:19:31 EDT 2006


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. However, any object I put into 
the 'space' in the PythonInitHandler was disappearing by the time the main 
PythonHandler started.

I traced it down to the fact that threading.local was presenting a different 
__dict__ to the PythonInitHandler from the PythonHandler. However, they're 
both running in the same thread, one after the other. Somewhere between the 
PythonInitHandler and the rest of the code, the threading.local object 
decides to switch the __dict__. It doesn't clear the __dict__, it is a 
different instance (the id changes).

Does anyone know of a reason when the this might happen? Does mod_python do 
something under the bonnet to confuse threading.local? I have a workaround 
(do everything in the PythonHandler), but I'd still like to know the root 

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!

I'm running Python 2.4.3, Apache 2.0.58, and mod_python 3.1.4 on Gentoo.


Gavin Panella.

Premolo Sàrl
Web: http://www.premolo.com/ ; Tel: +352 26687161 ; GSM: +352 091739359
42 rue du Kiem, L-1857 Luxembourg, Grand-Duché du Luxembourg

More information about the Mod_python mailing list