0

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.

bad
  • 56
  • 9
  • The code is not valid due to indentation problems, and I do not see where you set `found`. – Fred Larson Aug 08 '18 at 21:36
  • That happened when I pasted into stack overflow. The code does not throw an error. found is default false and is set to true in check. – bad Aug 08 '18 at 21:40
  • But you don't show `check()`. I suspect a scope error. – Fred Larson Aug 08 '18 at 21:41
  • Showing check now. It is not a scope error as the program will run with seemingly no problems _until_ it has to stop, where it doesn't – bad Aug 08 '18 at 21:46
  • 2
    Shouldn't `found` be `global` then? – Isaac Aug 08 '18 at 21:47
  • Yes, unless you add a `global found`, you're defining a new `found` that is local to `check()`. – Fred Larson Aug 08 '18 at 21:49
  • @Isaac is right. See also https://stackoverflow.com/a/10588342/1076479 – Gil Hamilton Aug 08 '18 at 21:50
  • If you're hoping for parallelism (improved performance over single threaded) and you're using CPython (aka normal Python) then threads won't help you. Look into the Multiprocessing module instead. – eddiewould Aug 08 '18 at 21:51
  • @Isaac it is global, let me clarify that. When I run a test printing found every loopThrough iteration, running check() doesn't set found to true for some reason. – bad Aug 08 '18 at 22:23
  • Hey guys, this was a simple mistake on my part. Sorry for wasting your time. My question still stands about the queue if you could help me there. @eddiewould Could you explan what you mean when you say it wont help me? Going through this word list with 4 threads is 4 times faster, I checked the speed. – bad Aug 08 '18 at 22:23
  • @bad read up on the global interpreter lock - https://wiki.python.org/moin/GlobalInterpreterLock – eddiewould Aug 09 '18 at 03:03

0 Answers0