Python Glossary Русский

Python 3.9+Linuxsignalfunction

signal.pidfd_send_signal()

sending signals

Sends a signal to a process through a pidfd file descriptor instead of a PID. Linux 5.1+ only.

Syntax

signal.pidfd_send_signal(pidfd, sig, siginfo=None, flags=0)

The point is protection against PID reuse: os.kill with an old number can hit someone else's process, while a pidfd is bound to one process forever — if it has exited you get ProcessLookupError. Get the descriptor with os.pidfd_open(pid) and close it with os.close. The siginfo and flags arguments are not supported yet: only None and 0.

Examples

import os, signal
got = []
old = signal.signal(signal.SIGUSR1, lambda s, f: got.append(s))
fd = os.pidfd_open(os.getpid())  # → ? нужен Linux 5.1+
signal.pidfd_send_signal(fd, signal.SIGUSR1)
os.close(fd)
signal.signal(signal.SIGUSR1, old)
print(got == [signal.SIGUSR1])  # → True

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy