ptomato
ptomato

Reputation: 57854

Can you join a Python queue without blocking?

Python's Queue has a join() method that will block until task_done() has been called on all the items that have been taken from the queue.

Is there a way to periodically check for this condition, or receive an event when it happens, so that you can continue to do other things in the meantime? You can, of course, check if the queue is empty, but that doesn't tell you if the count of unfinished tasks is actually zero.

Upvotes: 0

Views: 972

Answers (1)

Tommy
Tommy

Reputation: 620

The Python Queue itself does not support this, so you could try the following

from threading import Thread

class QueueChecker(Thread):
    def __init__(self, q):
        Thread.__init__(self)
        self.q = q

    def run(self):
        q.join()

q_manager_thread = QueueChecker(my_q)
q_manager_thread.start()

while q_manager_thread.is_alive():
    #do other things

#when the loop exits the tasks are done
#because the thread will have returned 
#from blocking on the q.join and exited 
#its run method

q_manager_thread.join() #to cleanup the thread

a while loop on the thread.is_alive() bit might not be exactly what you want, but at least you can see how to asynchronously check on the status of the q.join now.

Upvotes: 3

Related Questions