signal.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. """
  2. Cooperative implementation of special cases of :func:`signal.signal`.
  3. This module is designed to work with libev's child watchers, as used
  4. by default in :func:`gevent.os.fork` Note that each ``SIGCHLD`` handler
  5. will be run in a new greenlet when the signal is delivered (just like
  6. :class:`gevent.hub.signal`)
  7. The implementations in this module are only monkey patched if
  8. :func:`gevent.os.waitpid` is being used (the default) and if
  9. :const:`signal.SIGCHLD` is available; see :func:`gevent.os.fork` for
  10. information on configuring this not to be the case for advanced uses.
  11. .. versionadded:: 1.1b4
  12. """
  13. from __future__ import absolute_import
  14. from gevent._util import _NONE as _INITIAL
  15. from gevent._util import copy_globals
  16. import signal as _signal
  17. __implements__ = []
  18. __extensions__ = []
  19. _child_handler = _INITIAL
  20. _signal_signal = _signal.signal
  21. _signal_getsignal = _signal.getsignal
  22. def getsignal(signalnum):
  23. """
  24. Exactly the same as :func:`signal.signal` except where
  25. :const:`signal.SIGCHLD` is concerned.
  26. For :const:`signal.SIGCHLD`, this cooperates with :func:`signal`
  27. to provide consistent answers.
  28. """
  29. if signalnum != _signal.SIGCHLD:
  30. return _signal_getsignal(signalnum)
  31. global _child_handler
  32. if _child_handler is _INITIAL:
  33. _child_handler = _signal_getsignal(_signal.SIGCHLD)
  34. return _child_handler
  35. def signal(signalnum, handler):
  36. """
  37. Exactly the same as :func:`signal.signal` except where
  38. :const:`signal.SIGCHLD` is concerned.
  39. .. note::
  40. A :const:`signal.SIGCHLD` handler installed with this function
  41. will only be triggered for children that are forked using
  42. :func:`gevent.os.fork` (:func:`gevent.os.fork_and_watch`);
  43. children forked before monkey patching, or otherwise by the raw
  44. :func:`os.fork`, will not trigger the handler installed by this
  45. function. (It's unlikely that a SIGCHLD handler installed with
  46. the builtin :func:`signal.signal` would be triggered either;
  47. libev typically overwrites such a handler at the C level. At
  48. the very least, it's full of race conditions.)
  49. .. note::
  50. Use of ``SIG_IGN`` and ``SIG_DFL`` may also have race conditions
  51. with libev child watchers and the :mod:`gevent.subprocess` module.
  52. .. versionchanged:: 1.2a1
  53. If ``SIG_IGN`` or ``SIG_DFL`` are used to ignore ``SIGCHLD``, a
  54. future use of ``gevent.subprocess`` and libev child watchers
  55. will once again work. However, on Python 2, use of ``os.popen``
  56. will fail.
  57. .. versionchanged:: 1.1rc2
  58. Allow using ``SIG_IGN`` and ``SIG_DFL`` to reset and ignore ``SIGCHLD``.
  59. However, this allows the possibility of a race condition if ``gevent.subprocess``
  60. had already been used.
  61. """
  62. if signalnum != _signal.SIGCHLD:
  63. return _signal_signal(signalnum, handler)
  64. # TODO: raise value error if not called from the main
  65. # greenlet, just like threads
  66. if handler != _signal.SIG_IGN and handler != _signal.SIG_DFL and not callable(handler):
  67. # exact same error message raised by the stdlib
  68. raise TypeError("signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object")
  69. old_handler = getsignal(signalnum)
  70. global _child_handler
  71. _child_handler = handler
  72. if handler == _signal.SIG_IGN or handler == _signal.SIG_DFL:
  73. # Allow resetting/ignoring this signal at the process level.
  74. # Note that this conflicts with gevent.subprocess and other users
  75. # of child watchers, until the next time gevent.subprocess/loop.install_sigchld()
  76. # is called.
  77. from gevent import get_hub # Are we always safe to import here?
  78. _signal_signal(signalnum, handler)
  79. get_hub().loop.reset_sigchld()
  80. return old_handler
  81. def _on_child_hook():
  82. # This is called in the hub greenlet. To let the function
  83. # do more useful work, like use blocking functions,
  84. # we run it in a new greenlet; see gevent.hub.signal
  85. if callable(_child_handler):
  86. # None is a valid value for the frame argument
  87. from gevent import Greenlet
  88. greenlet = Greenlet(_child_handler, _signal.SIGCHLD, None)
  89. greenlet.switch()
  90. import gevent.os
  91. if 'waitpid' in gevent.os.__implements__ and hasattr(_signal, 'SIGCHLD'):
  92. # Tightly coupled here to gevent.os and its waitpid implementation; only use these
  93. # if necessary.
  94. gevent.os._on_child_hook = _on_child_hook
  95. __implements__.append("signal")
  96. __implements__.append("getsignal")
  97. else:
  98. # XXX: This breaks test__all__ on windows
  99. __extensions__.append("signal")
  100. __extensions__.append("getsignal")
  101. __imports__ = copy_globals(_signal, globals(),
  102. names_to_ignore=__implements__ + __extensions__,
  103. dunder_names_to_keep=())
  104. __all__ = __implements__ + __extensions__