Skip to main content

Launch, monitor, and clean up detached external processes with on-disk status tracking (no broker or worker required).

Project description

ox_proc

Launch, monitor, and clean up detached external processes with simple on-disk status tracking — no message broker, no persistent worker process.

ox_proc is aimed at the common case where a short-lived caller (e.g., a Flask/gunicorn request handler) needs to kick off a long-running command, return immediately, and let any later process check progress, read recent output, fetch the final result, or kill the run.

How it works

Each launch gets a run ID like myslug-20260611T143022-a1b2 and a run directory:

<base_dir>/<slug>/<run_id>/
    status.json     written by the launcher; finalized by observers
    messages.jsonl  appended by the child via post_message()
    result.json     written by the child via write_result()
    stdout.log      child's stdout
    stderr.log      child's stderr

The child is launched in its own session (setsid), so it survives the launcher's exit. Liveness checks verify both the PID and the process creation time recorded at launch, so a reused PID is never mistaken for the original process. No file locking is needed: status.json and result.json use atomic write-and-rename, and messages.jsonl is append-only.

Quick start

Launcher side (e.g., in a web request handler):

import ox_proc

run_id = ox_proc.launch(
    ["python", "-m", "myproject.analysis", "--full"],
    slug="analysis",
    description="Nightly full analysis",
    env_updates={"ANALYSIS_MODE": "full"},
    max_live=1,            # raise TooManyLiveError if one is running
)

Child side (inside the launched command):

import ox_proc

ox_proc.post_message("loading data", progress=0.1)
...
ox_proc.write_result({"rows": 12345, "ok": True})   # always on success

Status side (any process, any time):

info = ox_proc.get_info(run_id)
info["state"]          # "running", "finished", "died", or "killed"
info["messages"]       # recent post_message() records
info["stdout_tail"]    # last lines of stdout
info["result"]         # contents of result.json, or None

ox_proc.count_live()   # {"analysis": 1, ...}
ox_proc.kill_run(run_id)
ox_proc.cleanup()      # call periodically: kills over-runtime runs,
                       # deletes expired finished runs

Conventions and caveats

  • Always call write_result() on success. Because the child is detached, its true exit code is lost; a dead process with no result.json is reported as state "died".
  • End times are observed: recorded when an observer first notices the process is gone. TTL-based deletion counts from that time, with a backstop (default 24 h from launch) for runs whose end was never observed.
  • Live runs are killed by cleanup() once they exceed their per-run max_runtime_seconds (default 8 h; pass None for unlimited).
  • kill_run() sends SIGTERM to the whole process group; there is no SIGKILL escalation.
  • The default base directory is tempfile.gettempdir()/ox_proc-<username>, which the OS may purge (reboots, tmpfiles cleaning) — run history is not durable. Pass base_dir= everywhere for a durable location.
  • POSIX only (relies on sessions/process groups and atomic appends).
  • The max_live limit is best-effort: two simultaneous launches can race past it.

Installation

pip install ox_proc

Requires Python 3.9+ and psutil.

Development

pip install -e ".[dev]"
pytest

To build for pypi, first build via

pip install --upgrade build twine
python -m build

and then push to test pypi via:

twine upload --repository testpypi dist/*

and finally if that looks good you can upload to the production pypi via

twine upload dist/*

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

ox_proc-0.1.4.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

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

ox_proc-0.1.4-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file ox_proc-0.1.4.tar.gz.

File metadata

  • Download URL: ox_proc-0.1.4.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for ox_proc-0.1.4.tar.gz
Algorithm Hash digest
SHA256 6a330c2c35717a0d8dbc9db1811f9fcf52ddfb2914cb1ef2ee8df7f25650831d
MD5 4aae7aec337430725e01bd7cf77a1ddc
BLAKE2b-256 bb058b5ead4af1fd5c87e9cde3463782f5a906a932fee24c5be73e6a19aac95b

See more details on using hashes here.

File details

Details for the file ox_proc-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: ox_proc-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for ox_proc-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 7a28248a41843c6f6fdede067d6b4f6374f712a1dd6d5682b94a0e08402d8d07
MD5 bf344baa5c319bc91c3feae92845d8b4
BLAKE2b-256 7c5c16111fede2a74f646c587057c5b6c8194c3fd278bb576961c58d05dc54af

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