[mod_python] Background threads in mod_python

Mike Looijmans nlv11281 at natlab.research.philips.com
Mon Jul 31 10:57:44 EDT 2006


It really depends on WHAT you intend to do. I can think of no reason whatsoever to start a thread in 
the handler that somehow "survives" the request. Once that thread has created some "answer", where 
can it send that answer?

When Apache is using multiple processes, it will terminate child processes for various reasons (for 
example, when max_requests_per_child has been reached). That will also terminate any thread you 
created in that process.

I suspect your software gets a "job" from the user, and reports back to the user that the job has 
been started. The job itself must keep running for some time after that. The job creates a file or 
something similar which can make the user conclude that the job has finished.

A web service used within a web server is not redundant, it's just a way to delegate tasks to other 
machines or processes. This is typically done for security reasons.


Mike Looijmans
Philips Natlab / Topic Automation


Christian Gross wrote:
> Hmm... Are you not then making the argument for fastcgi? As much as I 
> would like to create a separate process, I am tempted to believe that 
> maybe I should not be using Apache in the first place.
> 
> Let me give you a context. I am using Ajax and Web Services to create a 
> SOA package. The Web Services are provided by mod_python. If I have to 
> delegate to a "web service" I am being redundant, no? In contrast, if I 
> were to use ASP.NET, or Java Servlets there can be as many background 
> threads as necessary and they will not be shot off.
> 
> Getting back to Apache. If I am using a threaded model, and Apache kills 
> off a request thread, that should not affect a thread I created in 
> Python, right? I can understand if the process is shot off, but threads 
> within a process should be independent of each other, no?
> 
> Christian
> 
> Mike Looijmans wrote:
> 
>> A much safer option is to create a separate process for this kind of 
>> thing. Using the extremely simple XMLRPC package (built-in in Python) 
>> you can do calls into the "worker" process.
>>
>> Apache will terminate your processes/threads at unexpected moments, 
>> since it expects all handlers to be stateless and only act in response 
>> to incoming requests. It may work on some configurations, but there's 
>> no guarantee that your application will behave the same on other systems.
>>
>>
>> Mike Looijmans
>> Philips Natlab / Topic Automation
>>
>>
>> Christian Gross wrote:
>>
>>> Hi,
>>>
>>> I have been using mod_python and am wondering about creating
>>> background threads. I can create the background thread, but if the
>>> thread requires a longer processing time than the request the
>>> background thread is exited.
>>>
>>> Is it possible to create a background thread and have it execute
>>> without being killed?
>>>
>>> Thanks
>>>
>>> Christian Gross
>>> _______________________________________________
>>> 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