title: Can I use Python Exceptions to handle the aftermath of SIGHUP signals for a daemon?
body: <p>I'm writing a simple little file-syncing daemon in python to implement a synchronised file system between a primary and secondary machine. Most of the heavy lifting is done by rsync.</p>
  
  <p>On the primary side it periodically calls rsync and sleeps for a few seconds before repeating. On the secondary side it spawns rsyncd with subprocess.Popen() and does a .wait() until it exits. However I want to trigger a reconfiguration of the daemon with a SIGHUP. I'm wondering what the best way of handling clean-up is.</p>
  
  <p>My initial thoughts were to have the signal handler raise an exception that can trigger the clean-up:</p>
  
  <pre><code>def signal_handler(signum, frame):
      raise fsync_config_exception
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>rsync_args = [rsync_binary, "--daemon", "--no-detach", "--config=%s" % (config.name) ]
  p = subprocess.Popen(rsync_args)
  try:
      p.wait()
      if p.returncode != 0:
          print "failed to spawn rsyncd"
          return False
  except fsync_config_exception:
      print "spawn_and_monitor_rsyncd: config exceptions"
  except:
      (type, value, tb) = sys.exc_info()
      print "we got %s with %s instead" % (type, value)
  </code></pre>
  
  <p>However I get a:</p>
  
  <pre><code>we got &lt;type 'exceptions.TypeError'&gt; with __init__() takes exactly 2 arguments (1 given) instead
  </code></pre>
  
  <p>instead of the expected fsync_config_exception. Has anyone any suggestions for the best approach to this sort of problem? I'm I hideously abusing Exceptions by trying to raise them from a signal context?</p>