Queue module example does not use task_completed on None messages
https://docs.python.org/3/library/queue.html#queue-objects (version 3.7.4) That leads to the confusing result that if, after putting the None on the queue, the main thread tries another q.join(), it blocks forever. def worker(): while True: item = q.get() if item is None: q.task_done # <== ADDED break do_work(item) q.task_done() q = queue.Queue()threads = []for i in range(num_worker_threads): t = threading.Thread(target=worker) t.start() threads.append(t) for item in source(): q.put(item) # block until all tasks are doneq.join() # stop workersfor i in range(num_worker_threads): q.put(None)for t in threads: t.join() It might be altogether more natural to write the worker as: def worker(): go_on = True while go_on: item = q.get() if item is None: go_on = False else do_work(item) q.task_done() That makes it clear that all tasks have to be acquitted in order for the queue to be clear on what is going on and when all is finished. Best --rck
participants (1)
-
Robert C Kahlert