Skip to main content

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's wait()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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

watchpid-0.1.1.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

watchpid-0.1.1-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file watchpid-0.1.1.tar.gz.

File metadata

  • Download URL: watchpid-0.1.1.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.2

File hashes

Hashes for watchpid-0.1.1.tar.gz
Algorithm Hash digest
SHA256 53cba3648cbbae9e02aaae0862d38d118e1d46f6f9cf049d3cb27f156f4bd7c3
MD5 8259e294f19d18c46b1c293c340f8643
BLAKE2b-256 b4bbfdb206894fdc337c3b5b57893aebcb644c0c78b6af8c8430d3dc45ba327e

See more details on using hashes here.

File details

Details for the file watchpid-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: watchpid-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.2

File hashes

Hashes for watchpid-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e2325efeccb0dadea2c39de09c83cc54b5c003eba2e9d49e4c9d6ebbb5960b8e
MD5 d166489303c43290a48698d9461b45a1
BLAKE2b-256 8e4886a11703feb912b9b933b5b0bd77da54d9d13223e27799923cdfc77ba602

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page