Skip to main content

Small stdlib-only helpers for watching process exit.

Project description

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

Project details


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.0.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.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for watchpid-0.1.0.tar.gz
Algorithm Hash digest
SHA256 53cdb24446f92e6ed2dcf969edf1a6c3607fe3bfaa9f88d9d2ede0bc8b73106d
MD5 c8327ee920850a193b4427005cd497a6
BLAKE2b-256 ab6512f024e0661820516583ed05b4488bb082b8cc12c9b90927227345937cbc

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for watchpid-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6002dbf6b76294ae87d08e56f903b5f257daf66ec8a41827fc7c6a20150bc048
MD5 d3bc1a57939f989c2fe5b609e43ee4b9
BLAKE2b-256 efa53eb96be7f3ed853735d51eab82708464ca2ab2440cd4a41bee0b24303a72

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