ossignal.py 994 B

1234567891011121314151617181920212223242526272829
  1. from __future__ import absolute_import
  2. import signal
  3. from twisted.internet import reactor
  4. signal_names = {}
  5. for signame in dir(signal):
  6. if signame.startswith('SIG') and not signame.startswith('SIG_'):
  7. signum = getattr(signal, signame)
  8. if isinstance(signum, int):
  9. signal_names[signum] = signame
  10. def install_shutdown_handlers(function, override_sigint=True):
  11. """Install the given function as a signal handler for all common shutdown
  12. signals (such as SIGINT, SIGTERM, etc). If override_sigint is ``False`` the
  13. SIGINT handler won't be install if there is already a handler in place
  14. (e.g. Pdb)
  15. """
  16. reactor._handleSignals()
  17. signal.signal(signal.SIGTERM, function)
  18. if signal.getsignal(signal.SIGINT) == signal.default_int_handler or \
  19. override_sigint:
  20. signal.signal(signal.SIGINT, function)
  21. # Catch Ctrl-Break in windows
  22. if hasattr(signal, 'SIGBREAK'):
  23. signal.signal(signal.SIGBREAK, function)