watchpid
watchpid is a tiny stdlib-only package for waiting until a process exits.
It uses OS process notifications when Python exposes them:
- Linux:
os.pidfd_open() - macOS/BSD:
select.kqueue() - Other platforms: polling fallback
Usage
Block until a pid exits:
from watchpid import wait_pid
if wait_pid(1234, timeout=5):
print("exited")
Run a callback in a daemon thread:
from watchpid import watch_pid
watch_pid(1234, lambda: print("exited"))
Watch the parent pid set by jupyter_client:
from watchpid import watch_parent
watch_parent(lambda: print("parent exited"))
watch_parent() reads JPY_PARENT_PID by default. It returns None when the env var is unset, invalid, or <= 1 (so pid 1/init is never watched).
Caveats
- The event-based backends (
pidfd,kqueue) detect exit reliably. The polling fallback uses pid liveness checks, so it is best-effort: it can't distinguish a reused pid, and an unreaped child process stays "alive" to the poller until it'swait()ed.
API
wait_pid(pid, timeout=None, poll=0.1) -> bool
watch_pid(pid, callback, timeout=None, poll=0.1, daemon=True, name=None) -> Thread
parent_pid(env="JPY_PARENT_PID") -> int | None
watch_parent(callback, env="JPY_PARENT_PID", timeout=None, poll=0.1, daemon=True, name="watchpid-parent") -> Thread | None
wait_pid() returns True when the process exits and False on timeout.
Development
pip install -e ".[test]"
pytest -q
Release files for watchpid 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| watchpid-0.1.1.tar.gz | 8.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| watchpid-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.3 kB
Release files / watchpid-0.1.1.tar.gz
| Download URL | watchpid-0.1.1.tar.gz |
|---|---|
| Size | 8.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
53cba3648cbbae9e02aaae0862d38d118e1d46f6f9cf049d3cb27f156f4bd7c3
|
|
BLAKE2b-256 checksum How to use checksums |
b4bbfdb206894fdc337c3b5b57893aebcb644c0c78b6af8c8430d3dc45ba327e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Release files / watchpid-0.1.1-py3-none-any.whl
| Download URL | watchpid-0.1.1-py3-none-any.whl |
|---|---|
| Size | 7.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e2325efeccb0dadea2c39de09c83cc54b5c003eba2e9d49e4c9d6ebbb5960b8e
|
|
BLAKE2b-256 checksum How to use checksums |
8e4886a11703feb912b9b933b5b0bd77da54d9d13223e27799923cdfc77ba602
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|