Help on thread pool

Jeff jeffober at gmail.com
Sat May 17 08:23:32 EDT 2008


Your worker threads wait around forever because there is no place for
them to exit.  Queue.get() by default blocks until there is an item in
the queue available.  You can do something like this to cause the
worker to quit when the queue is empty.  Just make sure that you fill
the queue before starting the worker threads.

from Queue import Queue, Empty

# in your worker
while True:
  try:
    item = q.get(block=False)
  except Empty:
    break
  do_something_with_item()
  q.task_done()

You can also use a condition variable and a lock or a semaphore to
signal the worker threads that all work has completed.



More information about the Python-list mailing list