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
& returnTrue
.
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")