The official documentation here gives the following example:
def worker():
while True:
item = q.get()
do_work(item)
q.task_done()
q = Queue()
for i in range(num_worker_threads):
t = Thread(target=worker)
t.daemon = True
t.start()
for item in source():
q.put(item)
q.join() # block until all tasks are done
I want to make sure all the threads are killed at this point before my main thread proceeds. I suppose after all the tasks in the queue have been processed, the q.get() method will raise an exception, which should kill the thread. Is that correct?
No. If there are no items in the queue,
getwill, by default, wait for items to be put into the queue. If you want it to raise an exception when there are no more items, either pass itblock=Falseor useget_nowait.Once you’re using the non-blocking
get, it should all work, but it is rather inelegant for your threads to die because of a raised exception in the normal case. I’d recommend surrounding it with atryblock, and if it throws an exception due to the queue being empty, stop the thread cleanly: