python threading with sync queue
Question: I have a script that follows the same logic in this sample. Basically I insert
items into a global queue and spawn threads with a while loop that gets and
item from the queue and the calls task_done.

I can get the threads to join if my while loop is checking that the queue is
not empty, but I wanted to try and incorporate a flag that I could set myself
to exit the loop. When I try to do this, joining the thread blocks forever.

Here is the non-working sample that doesnt join the threads:

    
    
    import threading
    import queue
    
    class Mythread(threading.Thread):
        def __init__(self):
            super().__init__()
            self.signal = False
        def run(self):
           global queue
           while not self.signal:
               item = q.get()
               print(item)
               q.task_done()
        def stop(self):
           self.signal = True
    
    q = queue.Queue
    for i in range(5000):
       q.put(i)
    
    threads = []
    for i in range(2):
        t = Mythread()
        threads.append(t)
    
    for t in threads:
        t.start()
    q.join()
    
    for t in threads:
        print(t.signal)   <---- False
        t.stop()             
        print(t.signal)   <---- True
        t.join()          <---- Blocks forever
    

Here is the one that works using queue empty

    
    
    import threading
    import queue
    
    class Mythread(threading.Thread):
        def __init__(self):
            super().__init__()
        def run(self):
            global queue
            while not q.empty():
                item = q.get()
                print(item)
                q.task_done()
    
    q = queue.Queue
    for i in range(5000):
       q.put(i)
    
    threads = []
    for i in range(2):
        t = Mythread()
        threads.append(t)
    
    for t in threads:
        t.start()
    
    q.join()
    
    for t in threads:
        t.join()          <---- Works fine
        print(t.is_alive())   <--- returns False
    

Any ideas?


Answer: q.get blocks so it won't reach your while condition

