[mod_python] Background threads in mod_python

Graham Dumpleton grahamd at dscpl.com.au
Sat Jul 29 20:44:21 EDT 2006

On 29/07/2006, at 11:16 PM, Christian Gross wrote:
>> Alternatively, if the Apache child process doesn't crash, you  
>> might at least
>> cause a Python exception to occur because the state of something  
>> in the
>> request object isn't as expected. This exception would effectively  
>> cause the
>> thread to exit unless the context in which the access was made to  
>> the request
>> object was protected by a try/except block.
> I copied specific items from the request object to a Python object.  
> Though this raises a question. If for example I assign the req.uri  
> to a class instance data member is this ok? Will at the end of the  
> request the data referenced by req.uri be released, or is req.uri a  
> buffer managed by Python?

Accessing a member of the request object and storing it is okay, as  
the data
is effectively copied in order to turn it into a Python object.

>> In the above I talk about a Python exception occurring due to  
>> access to a
>> no longer valid request object, but even an uncaught exception in  
>> your code
>> will cause the same thing, with the thread appearing to exit but  
>> with nothing
>> necessarily being logged anywhere. What you want to do is to put a  
>> try/except
>> block around everything the thread does and if an exception  
>> occurs, then
>> use apache.log_error() to log something to the Apache error log  
>> indicating
>> that the exception occurs and the details of the exception.
> Would you consider doing this as a general rule in mod_python? I  
> can see the advantages, but am wondering if that could be  
> considered a good programming practice?

For a separate thread, you would certainly have to deal with unexpected
exceptions in some way. If you get an unexpected exception in the  
handler however,  then it is caught by mod_python and turned into a 500
error response, with details if PythonDebug is set to On. Thus, only the
separate thread where you would have to be particularly mindful of it.


More information about the Mod_python mailing list