信号

Signals

with trio.open_signal_receiver(*signals) as signal_aiter

A context manager for catching signals.

Entering this context manager starts listening for the given signals and returns an async iterator; exiting the context manager stops listening.

The async iterator blocks until a signal arrives, and then yields it.

Note that if you leave the with block while the iterator has unextracted signals still pending inside it, then they will be re-delivered using Python's regular signal handling logic. This avoids a race condition when signals arrives just before we exit the with block.

参数:

signals (Signals | int) -- the signals to listen for.

抛出:
  • TypeError -- if no signals were provided.

  • RuntimeError -- if you try to use this anywhere except Python's main thread. (This is a Python limitation.)

示例

返回类型:

Generator[AsyncIterator[int], None, None]

A common convention for Unix daemons is that they should reload their configuration when they receive a SIGHUP. Here's a sketch of what that might look like using open_signal_receiver():

with trio.open_signal_receiver(signal.SIGHUP) as signal_aiter:
    async for signum in signal_aiter:
        assert signum == signal.SIGHUP
        reload_configuration()