multithreading

Peter Hansen peter at engcorp.com
Thu May 23 20:13:21 EDT 2002


Skip Montanaro wrote:
> 
>     >> > Can anyone point me to a useful tutorial on multithreading?
> 
>     >> One advice: Avoid multithreading like the plague.
> 
>     Cliff> I think this is a bit of an overstatement.  Many problems are
>     Cliff> best expressed as multithreaded programs.
> 
> I agree.  I have an xml-rpc server that was single-threaded for several
> years.  I always meant to multi-thread it, but I feared introducing
> instability in the server.  When I finally bit the bullet I discovered it
> wasn't all that difficult.  You have to exercise care to make sure all your
> shared objects are protected by locks or are contained in Queue objects.
> You may also have to internally cap the number of active threads.  For
> example, my xml-rpc server creates a thread to handle each connection.  The
> number of threads is proportional to the number of requests coming into the
> websites it serves.  The number of active threads is effectively capped by a
> Queue object which contains a set of cached MySQLdb connection objects.

Cool!  Do you mean you prepare the Queue ahead of time with a fixed
number of those objects, then any attempt to create a new thread blocks
on the Queue until a previous thread has terminated and released its
resource back into the Queue with a put(), presumably inside a 'finally'
block?

I think something like that would have simplified a little server we
just wrote.  Never thought of that approach before.

-Peter



More information about the Python-list mailing list