I am trying to implement a multithreaded producer-consumer pattern using Queue.Queue in Python 2.7. I am trying to figure out how to make the consumers, i.e. the worker threads,
Don't call it a special case for a task.
Use an Event instead, with non-blocking implementation for your workers.
stopping = threading.Event()
def worker(n, q, timeout=1):
# run until the master thread indicates we're done
while not stopping.is_set():
try:
# don't block indefinitely so we can return to the top
# of the loop and check the stopping event
data = q.get(True, timeout)
# raised by q.get if we reach the timeout on an empty queue
except queue.Empty:
continue
q.task_done()
def master():
...
print 'waiting for workers to finish'
q.join()
stopping.set()
print 'done'