platform.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import signal
  2. try:
  3. from setproctitle import setproctitle as _setproctitle
  4. except ImportError:
  5. _setproctitle = None
  6. def reset_signal(signal_name):
  7. """Reset signal to the default signal handler.
  8. Does nothing if the platform doesn't support signals,
  9. or the specified signal in particular.
  10. """
  11. try:
  12. signum = getattr(signal, signal_name)
  13. signal.signal(signum, signal.SIG_DFL)
  14. except AttributeError:
  15. pass
  16. def install_signal_handler(signal_name, handler):
  17. """Install a handler.
  18. Does nothing if the current platform doesn't support signals,
  19. or the specified signal in particular.
  20. """
  21. try:
  22. signum = getattr(signal, signal_name)
  23. signal.signal(signum, handler)
  24. except AttributeError:
  25. pass
  26. def set_process_title(progname, info=None):
  27. """Set the ps name for the currently running process.
  28. Only works if :mod`setproctitle` is installed.
  29. """
  30. if _setproctitle:
  31. proctitle = "[%s]" % progname
  32. proctitle = info and "%s %s" % (proctitle, info) or proctitle
  33. _setproctitle(proctitle)
  34. def set_mp_process_title(progname, info=None):
  35. """Set the ps name using the multiprocessing process name.
  36. Only works if :mod:`setproctitle` is installed.
  37. """
  38. from multiprocessing.process import current_process
  39. return set_process_title("%s.%s" % (progname, current_process().name),
  40. info=info)