title: Why does using threading.Event result in SIGTERM not being caught?
body: <p>I have a threaded Python daemon. Like any good daemon, it wants to launch all of its worker threads, then wait around until it's told to terminate. The normal signal for termination is <code>SIGTERM</code>, and in most languages I'd hold to terminate by waiting on an event or mutex, so using <code>threading.Event</code> made sense to me. The problem is that Python's <code>Event</code> object and Unix signals don't appear to be playing well together.</p>
  
  <p>This works as expected, terminating on <code>SIGTERM</code>:</p>
  
  <pre><code>import signal
  import time
  
  RUN = True
  
  def handle(a, b):
      global RUN
      print "handled"
      RUN = False
  
  signal.signal(signal.SIGTERM, handle)
  while RUN:
      time.sleep(0.250)
  print "Stopping"
  </code></pre>
  
  <p>but this results in no <code>SIGTERM</code> being delivered (i.e., quite apart from quitting, "handled" never gets printed):</p>
  
  <pre><code>import signal
  import threading
  
  RUN_EVENT = threading.Event()
  
  def handle(a, b):
      print "handled"
      RUN_EVENT.set()
  
  signal.signal(signal.SIGTERM, handle)
  RUN_EVENT.wait()
  print "Stopping"
  </code></pre>
  
  <p>So my question is:</p>
  
  <ol>
  <li>Am I misusing <code>threading.Event</code> in some way?</li>
  <li>If I am not, is there an alternative other than the poll-and-sleep mechanism from the first example?</li>
  <li>Also if I am not, why does using <code>threading.Event</code> kill the signal handler?</li>
  </ol>