Ending 15 years of subprocess polling
7 days ago
- #process-management
- #CPython
- #POSIX
- Process termination waiting in POSIX systems traditionally uses inefficient busy-loop polling.
- Busy-polling causes CPU wake-ups, latency, and scalability issues.
- Linux's pidfd_open() and BSD/macOS's kqueue() allow event-driven process waiting, eliminating busy-looping.
- Windows already uses event-driven waiting with WaitForSingleObject.
- Graceful fallbacks to polling are implemented if event-driven methods fail.
- Event-driven methods significantly reduce CPU context switches compared to polling.
- CPython's subprocess module now includes these optimizations, inspired by psutil's implementation.