launch.utilities.signal_management module

Module for signal management functionality.

class launch.utilities.signal_management.AsyncSafeSignalManager[source]

Bases: object

A context manager class for asynchronous handling of signals.

Similar in purpose to asyncio.loop.add_signal_handler() but not limited to Unix platforms.

Signal handlers can be registered at any time with a given manager. These will become active for the extent of said manager context. Unlike regular signal handlers, asynchronous signals handlers can safely interact with their event loop.

The same manager can be used multiple consecutive times and even be nested with other managers, as these are independent from each other i.e. managers do not override each other’s handlers.

If used outside of the main thread, a ValueError is raised.

The underlying mechanism is built around signal.set_wakeup_fd() so as to not interfere with regular handlers installed via signal.signal(). All signals received are forwarded to the previously setup file descriptor, if any.

..warning::

Within (potentially nested) contexts, signal.set_wakeup_fd() calls are intercepted such that the given file descriptor overrides the previously setup file descriptor for the outermost manager. This ensures the manager’s chain of signal wakeup file descriptors is not broken by third-party code or by asyncio itself in some platforms.

__init__(loop: AbstractEventLoop)[source]

Instantiate manager.

Parameters:

loop – event loop that will handle the signals.

handle(signum: Signals | int, handler: Callable[[int], None] | None) Callable[[int], None] | None[source]

Register a callback for asynchronous handling of a given signal.

Parameters:
  • signum – number of the signal to be handled

  • handler – callback taking a signal number as its sole argument, or None

Returns:

previous handler if any, otherwise None