FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-83069: Use efficient event-driven `subprocess.Popen.wait()` on Linux / macOS / BSD by giampaolo · Pull Request #144047 · python/cpython · GitHub

Repository navigation

gh-83069: Use efficient event-driven subprocess.Popen.wait() on Linux / macOS / BSD - #144047

Merged
vstinner merged 68 commits into
python:mainfrom
giampaolo:subprocess-fast-wait
Jan 28, 2026
Merged

vstinner merged 68 commits into
python:mainfrom
giampaolo:subprocess-fast-wait

Conversation

giampaolo commented Jan 19, 2026 •
edited
Loading

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

vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Nice change! I like it. Here is a first review.

cc @gpshead

Comment thread Lib/subprocess.py Outdated
Comment thread Lib/subprocess.py Outdated
Comment thread Doc/library/subprocess.rst Outdated
Comment thread Lib/test/test_subprocess.py Outdated
Comment thread Lib/test/test_subprocess.py Outdated
Comment thread Lib/test/test_subprocess.py Outdated
Comment thread Doc/whatsnew/3.15.rst Outdated

Copy link
Copy Markdown
Contributor Author

Thanks for the review @vstinner. I addressed your comments.

Comment thread Lib/subprocess.py Outdated

vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM. Thanks for the updates!

Comment thread Lib/test/test_subprocess.py Outdated
Comment thread Lib/test/test_subprocess.py Outdated

Copy link
Copy Markdown
Contributor Author

Whops! I fixed the remaining bare assert statements.

Copy link
Copy Markdown
Member

@gpshead: Do you want to double check the change?

giampaolo commented Jan 24, 2026 •
edited
Loading

Copy link
Copy Markdown
Contributor Author

In the hope of adding BSD / macOS support for asyncio in the future, I have opened #144211.

vstinner merged commit 9b154ab into python:main Jan 28, 2026
47 checks passed

Copy link
Copy Markdown
Member

Merged, thank you for this nice optimization!

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL