Python 3.3+Linux · macOSsignalfunction
signal.pthread_sigmask()
masks and waiting
Reads and changes the mask of blocked signals of the calling thread; returns the old mask. Unix only (Linux, macOS).
Syntax
old_mask = signal.pthread_sigmask(how, mask)
A blocked signal is not lost, it waits: after unblocking the handler fires at once, but several identical signals merge into one. The mask belongs to the thread, not the process, and new threads inherit it from their creator — that is how signals get blocked in all worker threads. To read the mask without changing it, call pthread_sigmask(SIG_BLOCK, []). SIGKILL and SIGSTOP cannot be blocked.
Examples
import signal
got = []
old = signal.signal(signal.SIGUSR1, lambda s, f: got.append(s))
signal.pthread_sigmask(signal.SIG_BLOCK, {signal.SIGUSR1})
signal.raise_signal(signal.SIGUSR1)
signal.raise_signal(signal.SIGUSR1)
print(len(got)) # → 0 — сигналы ждут
signal.pthread_sigmask(signal.SIG_UNBLOCK, {signal.SIGUSR1})
print(len(got)) # → 1 — два одинаковых слились в один
signal.signal(signal.SIGUSR1, old)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy