giampaolo · GitHub

Copy link Copy Markdown

Contributor

This patch replaces the busy-loop mechanism used by subprocess.Popen.wait() on Linux, BSD, and macOS when a timeout is specified:

  • Linux >= 5.3: uses os.pidfd_open() + select.poll()
  • macOS, BSD: uses select.kqueue() + KQ_FILTER_PROC + KQ_NOTE_EXIT

The kernel wakes us up exactly when the PID terminates or the timeout expires. If the fast-wait mechanisms is not available or fails (e.g. "too many open files"), the function falls back to the traditional busy loop. A similar approach was recently implemented in psutil (see giampaolo/psutil#2706). While working on that change, I noticed that the subprocess module used essentially the same busy-loop strategy, which motivated this proposal.

EDIT: I wrote a blog post about this: https://gmpy.dev/blog/2026/event-driven-process-waiting

Read the original on github.com ↗