I am trying to make a multithreaded python program that continuously loops through a queue object until a condition is met. I have one problem and another question. The problem is, after the condition should be met, some threads still run. Here is how I initialize the threads:
for i in range(0, number_of_threads):
print("Created thread: "+ str(i))
t = threading.Thread(target=loopThrough)
#t.daemon = True
t.start()
threads[i] = t
time.sleep(1)
This works, and allows all threads to call the function. Here is the function loopThrough:
def loopThrough():
global found
global word_queue
while not found:
if word_queue.empty():
word_queue = buildQ()
print("Built queue because it was empty")
word = word_queue.get()
if word == "happy":
check(word)
The check function (defined before loopThrough)
def check(word):
global found
found = True
print(len(word))
This is a simplification, as the actual critera/etc is irreverent and long. Here is my problem. When I call the check function, I set the found flag to true, so it doesn't loop anymore. However, the other threads still loop. What I ideally want to do is kill every thread, and use the main thread to call the check function.
Lastly, I have one other concern. I wanted to know how to make a queue object that simply takes the object that was just gotten and moves it to the end of the queue. I don't think building a queue like this is efficient.