2

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")
0

1 Answer 1

3

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")
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Roman, How do i get the return True ? , AttributeError: 'MazeRunner' object has no attribute 'check_completed' lets_try = MazeRunner() print(lets_try.check_completed())
@ArbazzHussain, see my update (the way you need to check your threads done)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.