use of Queue

Jeff jeffober at gmail.com
Wed Aug 27 07:54:22 EDT 2008


> Your solution works assuming that you know how many consumer threads
> you have :). I don't :). More than that, it's not correct if you have
> more than one producer :). Having a sentinel was my very first idea,
> but as you see... it's a race condition (there are cases in which not
> all items are processed).

Queue raises an Empty exception when there are no items left in the
queue.  Put the q.get() call in a try block and exit in the except
block.

You can also use a condition variable to signal threads to terminate.



More information about the Python-list mailing list