Simple example of threading

rmeegan at murlmail.com rmeegan at murlmail.com
Thu Feb 24 16:12:35 CET 2000


[Thomas Weholt]
> >
> >I want to use threading in a server-based search/index application. 
Any 
> >hints? The Python-docs didn`t help much. Tutorials, examples and 
code, 
> >pleez.

[Aahz]
> 
> Unfortunately, using threading in a server gets a bit more complex and
> may not buy you very much.  If you really want a threaded server, I
> suggest looking at Zope and Zserver -- they've already done all the 
hard 
> work.

There is something to be said for spawning new processes to handle 
server requests. In particular, parent processes can kill their 
children when they wander off and start misbehaving. Threads can't be 
killed by other threads, including the main thread of the process that 
spawned them. 

The cost (in milliseconds) of spawning a process in Linux is on the 
same order as that for spawning a new thread. This means that only the 
most time sensitive environments need worry about the extra overhead. 

Another advantage to spawning a seperate process depends on the nature 
of your code and your system. If you are creating a long-running server 
that uses a number of C extensions, there is the opportunity for memory 
leaks. Spawning a seperate process to handle all of the dirty work 
means that everything gets cleaned up at the end of the request. This 
is one of the reasons that Apache spwns and kills httpd processes to 
handle requests. 

As always, your mileage may vary. The best way to find out is to write 
a single threaded application first. This will give you a baseline for 
performance. If your request rate is low or the time required to 
process a request is short, you may not need anything else. If you find 
that you do, use your now thoroughly debugged code as a starting point 
and add either threading or process forking and see what happens. 

Good luck -
- Robert





More information about the Python-list mailing list