Python Glossary Русский

all Python 3signalfunction

signal.set_wakeup_fd()

handlers

Sets a descriptor that receives a byte with the signal number for every handled signal — used to wake up select and event loops.

Syntax

old_fd = signal.set_wakeup_fd(fd, *, warn_on_full_buffer=True)

The descriptor must be non-blocking, or a write from the handler may hang the process — usually a socket pair or os.pipe with os.set_blocking(fd, False). A byte is written only for signals that have a handler. There is one wakeup fd per process and asyncio already uses it: the function returns the previous one — restore it afterwards. It may only be called from the main thread.

Examples

import signal, socket
r, w = socket.socketpair()
w.setblocking(False)
old = signal.signal(signal.SIGTERM, lambda s, f: None)  # без обработчика байта не будет
print(signal.set_wakeup_fd(w.fileno()))  # → -1 — раньше не было
signal.raise_signal(signal.SIGTERM)
print(r.recv(1)[0] == signal.SIGTERM)  # → True
signal.set_wakeup_fd(-1)
signal.signal(signal.SIGTERM, old)

See also

Open in the full glossarydocs.python.orgReport an inaccuracy