Numbers of active threads

Skip Montanaro skip at pobox.com
Sat May 11 18:02:36 EDT 2002


    >> I use threading module and I need to control numbers of active
    >> threads. Can you please give me an example how to do that.

    Peter> "Control" could mean many things, so I'll just point you to
    Peter> threading.activeCount() which returns the number of threads
    Peter> active (that were started with that module).

    Peter> You should be able to use this information to control the number
    Peter> of threads in an appropriate way for your application.

A slightly different approach would be to allow as many threads to start as
want to, but fill a Queue.Queue object up with the number of objects that
corresponds to the maximum number of threads you want doing something.  Call
get() when the thread starts and put() before it returns.  I use this to cap
the number of active MySQLdb connections my system runs.  Obviously, this
won't work if the "natural" number of threads that might start is very high
(dozens to hundreds or more) when you only want a few 1-10 to actually be
doing something.  

-- 
Skip Montanaro (skip at pobox.com - http://www.mojam.com/)
"Excellant Written and Communications Skills [required]" - post to chi.jobs





More information about the Python-list mailing list