Python 3.3+Linux · macOSsignalfunction
signal.sigwait()
masks and waiting
Waits for one of the given signals, takes it off the pending set and returns its number; no handler is called. Unix only (Linux, macOS).
Syntax
signum = signal.sigwait(sigset)
It only works reliably when the awaited signals are blocked with pthread_sigmask beforehand: otherwise the signal goes to the handler (or kills the process) before sigwait sees it. There is no timeout — use sigtimedwait for a bounded wait (it is missing on macOS). The classic pattern is a dedicated thread calling sigwait in a loop and handling signals synchronously.
Examples
import signal
old = signal.pthread_sigmask(signal.SIG_BLOCK, {signal.SIGUSR1}) # сначала блок
signal.raise_signal(signal.SIGUSR1)
print(signal.sigwait({signal.SIGUSR1}).name) # → SIGUSR1 — сразу, сигнал уже ждал
signal.pthread_sigmask(signal.SIG_SETMASK, old)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy