Arbazz Hussain
Arbazz Hussain

Reputation: 1932

How to identify if Python Threads with Queue are done with task?

Here i have MazeRunner Class which put all elements of self.boxes in queue and run thread on them until all of the queue becomes empty q.empty() .

Here problem is how do i actually identify if my program is done performing threads on all elements which are in queue of self.boxes & return True.

It looks challenging because our threads are in while loop which keep changes based on self.boxes length & self.threads we defined. i have tried putting all threads in list and t.join them all. But not luck. Any Help?

import threading,queue,time 

class MazeRunner:
    def __init__(self):
        self.q = queue.Queue()
        self.boxes = [1,2,3,4,5,6,7] ## `7` elements of list
        self.threads = 5

        for i in self.boxes:
            self.q.put(i) ### ADDING Every element of list to queue

        for j in range(self.threads): ### for i in range(5)  threads
            t = threading.Thread(target=self.ProcessQueue)
            t.start() ### Started `5` threads on `7` elements

    def ProcessQueue(self):
        while not self.q.empty():
            each_element = self.q.get()
            self.SleepFunction(each_element)
            self.q.task_done()

    def SleepFunction(self,each_element):
        print("STARTING : ",each_element)
        time.sleep(10)
        print("DONE : ",each_element)

lets_try = MazeRunner()
if lets_try == True:
     print("All Threads Done on Elements")

Upvotes: 2

Views: 402

Answers (1)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

You need to wait until all threads are done calling Thread.join:

HOWTO:

  • Replace your self.threads = 5 expression with class constant:

    THREAD_NUM = 5
    
  • Put additional attribute threads (for a list of threads) into your __init__ method:

    ...
    self.threads = []
    
  • Put each created thread into threads list:

    for j in range(self.THREAD_NUM):
        t = threading.Thread(target=self.ProcessQueue)
        self.threads.append(t)
        t.start()
    
  • Define method like check_completed to ensure all threads are terminated (done):

    ....
    
    def check_completed(self):
        for t in self.threads:
            t.join()
        return True
    

The way you need to check "all done":

m_runner = MazeRunner()
if m_runner.check_completed():
    print("All Threads Done on Elements")

Upvotes: 3

Related Questions