t.i._signals : module documentation

Part of twisted.internet View Source

This module provides a uniform interface to the several mechanisms which are possibly available for dealing with signals.

This module is used to integrate child process termination into a reactor event loop. This is a challenging feature to provide because most platforms indicate process termination via SIGCHLD and do not provide a way to wait for that signal and arbitrary I/O events at the same time. The naive implementation involves installing a Python SIGCHLD handler; unfortunately this leads to other syscalls being interrupted (whenever SIGCHLD is received) and failing with EINTR (which almost no one is prepared to handle). This interruption can be disabled via siginterrupt(2) (or one of the equivalent mechanisms); however, if the SIGCHLD is delivered by the platform to a non-main thread (not a common occurrence, but difficult to prove impossible), the main thread (waiting on select() or another event notification API) may not wake up leading to an arbitrary delay before the child termination is noticed.

The basic solution to all these issues involves enabling SA_RESTART (ie, disabling system call interruption) and registering a C signal handler which writes a byte to a pipe. The other end of the pipe is registered with the event loop, allowing it to wake up shortly after SIGCHLD is received. See twisted.internet.posixbase._SIGCHLDWaker for the implementation of the event loop side of this solution. The use of a pipe this way is known as the self-pipe trick.

The actual solution implemented in this module depends on the version of Python. From version 2.6, signal.siginterrupt and signal.set_wakeup_fd allow the necessary C signal handler which writes to the pipe to be registered with SA_RESTART. Prior to 2.6, the twisted.internet._sigchld extension module provides similar functionality.

If neither of these is available, a Python signal handler is used instead. This is essentially the naive solution mentioned above and has the problems described there.

Class _Handler _Handler is a signal handler which writes a byte to a file descriptor whenever it is invoked.
Function _installHandlerUsingSignal Install a signal handler which will write a byte to fd when SIGCHLD is received.
Function _installHandlerUsingSetWakeup Install a signal handler which will write a byte to fd when SIGCHLD is received.
Function _isDefaultHandler Determine whether the SIGCHLD handler is the default or not.
Function _cannotInstallHandler Fail to install a signal handler for SIGCHLD.
Function _cannotDetermineDefault Undocumented
def _installHandlerUsingSignal(fd): (source)
Install a signal handler which will write a byte to fd when SIGCHLD is received.

This is implemented by creating an instance of _Handler with fd and installing it as the signal handler.

ParametersfdThe file descriptor to which to write when SIGCHLD is received. (type: int)
def _installHandlerUsingSetWakeup(fd): (source)
Install a signal handler which will write a byte to fd when SIGCHLD is received.

This is implemented by installing an instance of _Handler wrapped around None, setting the SIGCHLD handler as not allowed to interrupt system calls, and using signal.set_wakeup_fd to do the actual writing.

ParametersfdThe file descriptor to which to write when SIGCHLD is received. (type: int)
def _isDefaultHandler(): (source)
Determine whether the SIGCHLD handler is the default or not.
def _cannotInstallHandler(fd): (source)
Fail to install a signal handler for SIGCHLD.

This implementation is used when the supporting code for the other implementations is unavailable (on Python versions 2.5 and older where neither the twisted.internet._sigchld extension nor the standard signal module is available).

ParametersfdIgnored; only for compatibility with the other implementations of this interface.
RaisesRuntimeErrorAlways raised to indicate no SIGCHLD handler can be installed.
def _cannotDetermineDefault(): (source)
Undocumented
API Documentation for Twisted, generated by pydoctor at 2012-06-04 17:20:01.