Python 3.9+Linuxosfunction
os.pidfd_open
os — special descriptors
Return a file descriptor referring to a process by PID (3.9+). Availability: Linux.
Syntax
os.pidfd_open(...)
The descriptor refers to a specific process, not a number, so a signal sent via signal.pidfd_send_signal cannot hit another process that inherited the PID after the original exited. The descriptor works with select: it becomes readable when the process has exited.
Examples
import os, select, signal
fd = os.pidfd_open(os.getpid()) # → ? только Linux: дескриптор, привязанный к текущему процессу, например 3
signal.pidfd_send_signal(fd, 0) # → None — сигнал 0 лишь проверяет, что процесс жив (реальный SIGTERM шлют так же, но дочернему процессу)
print(select.select([fd], [], [], 0)) # → ([], [], []) — процесс ещё работает; когда завершится, fd станет готов к чтению
os.kill(os.getpid(), 0) # → ? только Unix: то же самое по PID — но освободившийся PID ОС может отдать другому процессу, а pidfd всегда указывает на тот самый
os.close(fd) # → ? только Linux: дескриптор закрыт; на сам процесс это никак не влияет
See also
Open in the full glossarydocs.python.orgReport an inaccuracy