title: Python: detect if the parent thread called exit()
body: <p>I have this thread that is running in the background and can be updated from the main thread:</p>
  
  <pre><code>import threading
  from Queue import Queue
  from time import sleep
  
  class Animation(threading.Thread):
  
      SIGNAL_STOP = 'stop'
  
      def __init__(self, framerate):
          threading.Thread.__init__(self)
          self.queue = Queue()
          self.framerate = framerate
  
      def tell(self, data):
          self.queue.put(data)
  
      def stop(self):
          self.tell(Animation.SIGNAL_STOP)
  
      def loop(self):
          # Override this method to implement animation loop
          pass
  
      def update(self, data):
          # Override this method to implement the state update
          pass
  
      def cleanup(self):
         # Override this method to implement what's done when the animation is stopped
         pass
  
      def run(self):
          while True:
              if not self.queue.empty():
                  data = self.queue.get()
                  if data == Animation.SIGNAL_STOP:
                      break;
                  self.update(data)
              self.loop()
              sleep(1. / self.framerate)
  
          self.cleanup()
  
  
  class TestAnimation(Animation):
      def __init__(self, framerate):
          super(TestAnimation, self).__init__(framerate)
          self.num = 0
  
      def loop(self):
          print 'num =', self.num
          self.num = self.num + 1
  
      def update(self, data):
          print 'update:', data
  
      def cleanup(self):
          print 'graceful exit'
  
  print 'start'
  ta = TestAnimation(1)
  ta.start()
  sleep(3)
  ta.update(123)
  sleep(3)
  #ta.stop() # I'd like the animation thread to feel that the parent wants to exit and carry out stopping itself
  print 'end'
  exit()
  </code></pre>
  
  <p>I'd like to implement some way to detect when the parent thread wants to exit and all running threads would then gracefully terminate themselves. I'd prefer that instead of explicitly calling the running thread's stop() method.</p>