Dependency-free Python bindings for Linux pidfds. A pidfd is a file descriptor referring to a process: a stable handle that can never hit a recycled pid, becomes readable when the task exits, can be waited on with waitid(2), and can duplicate the task's file descriptors with pidfd_getfd(2). Everything goes through pidfd_open(2), pidfd_send_signal(2), pidfd_getfd(2) and waitid(2) via ctypes. There are no runtime dependencies.
Requires Python 3.10+ and Linux.
pip install pidfd
import select, signal, subprocess
from pidfd import PidFd, WaitCode
proc = subprocess.Popen(["sleep", "30"])
with PidFd.open(proc.pid) as pf:
# poll(2) reports the fd readable once the task is a zombie
pf.signal(signal.SIGKILL)
poller = select.poll()
poller.register(pf.fileno(), select.POLLIN)
poller.poll()
result = pf.wait() # waitid(P_PIDFD)
assert result.code == WaitCode.CLD_KILLEDPidFd.open(pid, OpenFlag.NONBLOCK) makes wait() fail with EAGAIN
instead of blocking. pf.getfd(fd) duplicates one of the target's open
fds (ptrace access rules apply).
uv sync --group dev
make check
License: 0BSD. Quad4 Software, https://quad4.io