title: How do I send data to a running python thread?
body: <p>I have a class that is run in separate threads in my application. I can have multiple threads running at a time and the threads are daemons. After a period of time, <em>some</em> of these threads need to receive and process a message. How do I do this?</p>
  
  <p>A sample of my code looks like this:</p>
  
  <pre><code>import threading
  import time 
  
  class MyThread(threading.Thread):
      def __init__(self, args=(), kwargs=None):
          threading.Thread.__init__(self, args=(), kwargs=None)
          self.daemon = True
          self.receive_messages = args[0]
  
      def run(self):
          print threading.currentThread().getName(), self.receive_messages
  
      def do_thing_with_message(self, message):
          if self.receive_messages:
              print threading.currentThread().getName(), "Received %s".format(message)
  
  if __name__ == '__main__':
      threads = []
      for t in range(10):
          threads.append( MyThread(args=(t % 2 == 0,)))
          threads[t].start()
          time.sleep(0.1)
  
      for t in threads:
          t.do_thing_with_message("Print this!")
  </code></pre>
  
  <p>This outputs:</p>
  
  <pre><code>Thread-1 True
  Thread-2 False
  Thread-3 True
  Thread-4 False
  Thread-5 True
  Thread-6 False
  Thread-7 True
  Thread-8 False
  Thread-9 True
  Thread-10 False
  MainThread Received %s
  MainThread Received %s
  MainThread Received %s
  MainThread Received %s
  MainThread Received %s
  </code></pre>
  
  <p>I am expecting, however, those last five lines to not be related to the <code>MainThread</code>, and instead of <code>%s</code>, I'd expect it to me <code>Print this!</code>, like so:</p>
  
  <pre><code>Thread-1 True
  Thread-2 False
  Thread-3 True
  Thread-4 False
  Thread-5 True
  Thread-6 False
  Thread-7 True
  Thread-8 False
  Thread-9 True
  Thread-10 False
  Thread-1 Received Print this!
  Thread-3 Received Print this!
  Thread-5 Received Print this!
  Thread-7 Received Print this!
  Thread-9 Received Print this!
  </code></pre>
  
  <p>How can I properly send a message like this to the running threads?</p>
  
  <p><strong>Addendum:</strong></p>
  
  <p>If I have this block after the <code>Print this!</code> block, and utilize @dano's code to solve the problem above, it does not seem to respond to these new messages.</p>
  
  <pre><code>for t in threads:
      t.queue.put("Print this again!")
      time.sleep(0.1)
  </code></pre>
  
  <p>In this case, I'd expect the end of my output to look like this</p>
  
  <pre><code>Thread-1 Received Print this!
  Thread-3 Received Print this!
  Thread-5 Received Print this!
  Thread-7 Received Print this!
  Thread-9 Received Print this!
  Thread-1 Received Print this again!
  Thread-3 Received Print this again!
  Thread-5 Received Print this again!
  Thread-7 Received Print this again!
  Thread-9 Received Print this again!
  </code></pre>