title: python threading with sync queue
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>Here is the non-working sample that doesnt join the threads:</p>
  
  <pre><code>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)   &lt;---- False
      t.stop()             
      print(t.signal)   &lt;---- True
      t.join()          &lt;---- Blocks forever
  </code></pre>
  
  <p>Here is the one that works using queue empty</p>
  
  <pre><code>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()          &lt;---- Works fine
      print(t.is_alive())   &lt;--- returns False
  </code></pre>
  
  <p>Any ideas?</p>