signals.py 953 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. import signal
  4. from ..const import IS_WINDOWS_PLATFORM
  5. class ShutdownException(Exception):
  6. pass
  7. class HangUpException(Exception):
  8. pass
  9. def shutdown(signal, frame):
  10. raise ShutdownException()
  11. def set_signal_handler(handler):
  12. signal.signal(signal.SIGINT, handler)
  13. signal.signal(signal.SIGTERM, handler)
  14. def set_signal_handler_to_shutdown():
  15. set_signal_handler(shutdown)
  16. def hang_up(signal, frame):
  17. raise HangUpException()
  18. def set_signal_handler_to_hang_up():
  19. # on Windows a ValueError will be raised if trying to set signal handler for SIGHUP
  20. if not IS_WINDOWS_PLATFORM:
  21. signal.signal(signal.SIGHUP, hang_up)
  22. def ignore_sigpipe():
  23. # Restore default behavior for SIGPIPE instead of raising
  24. # an exception when encountered.
  25. if not IS_WINDOWS_PLATFORM:
  26. signal.signal(signal.SIGPIPE, signal.SIG_DFL)