I think it would be nice if queue.Queue.join()
method supported a current_tasks: bool
parameter: if True, join()
waits only for the tasks which have already been put into the queue to be completed.
There are situations when the arrival of new tasks is unpredictable and therefore the current join()
is unusable: join()
may never return, although semantically I just want to wait for tasks that have already been enqueued (if any).
The code change required to implement this is trivial: maintain two fields added_tasks
and done_tasks
instead of a single unfinished_tasks
, and then:
def join(self, current_tasks: bool) -> None:
with self.tasks_done_cond:
if not current_tasks:
# Wait until all tasks ever added are done
while self.done_tasks < self.added_tasks:
self.tasks_done_cond.wait()
else:
# Wait only until tasks added *before* this join() call are done
target_tasks = self.added_tasks
while self.done_tasks < target_tasks:
self.tasks_done_cond.wait()
# tasks_done_cond is renamed all_tasks_done
# Also use tasks_done_cond.notify_all() in Queue.task_done().