title: Exiting a thread in python?
body: <p>I have a program that pulls "work" from a database then dispatches it to workers.</p>
  
  <p>I have a thread dedicated to doing some work in a loop.  The work it does comes from a database, and the call to get a message is blocking:</p>
  
  <pre><code>class MessageThread(Thread):
      def __init__(self, db, worker_inbox, *args, **kwargs):
          super(MessageThread, self).__init__(*args, **kwargs)
          self.db = db
          self.worker_inbox = worker_inbox  # this is a stdlib Queue.Queue
  
      def run(self):
          while True:
              message = self.db.get()
              self.worker_inbox.put(message)
  </code></pre>
  
  <p>Without having to put some "flag" into the database, is there a nice way to stop this thread?  Currently I am setting the <code>daemon</code> flag on it, which kills it when the main thread exits, but I wondered if there was a nicer mechanism or way of designing this?</p>