Skip to main content

uppercut-subprocess-runner

Runs a command with a timeout, an output cap and cancellation, and returns a result object with the exit code, the captured output, the last bytes of each stream and how the run ended. When it has to stop the command it kills the whole process tree: the process group on macOS and Linux, a process-group break followed by taskkill /F /T on Windows. It sends a soft signal first, waits a grace period, then forces the kill. Standard library only.

pip install uppercut-subprocess-runner
import threading
from uppercut_subprocess_runner import run

cancel = threading.Event()  # call cancel.set() from another thread to stop the run

result = run(
    ["python", "build.py", "--fast"],
    cwd="project",
    timeout=120,
    max_output=2_000_000,
    grace=5,
    cancel=cancel,
)

if result.timed_out:
    print("gave up after", round(result.duration), "seconds")
elif not result.ok:
    print(result.error_tail())

Use check=True to raise CommandError instead, with the tail of the output in the message and the full result in error.result.

Functions

Function Returns
run(argv, **options) RunResult
kill_tree(process_or_pid, grace=5.0) KillInfo
popen_group(argv, **popen_options) a subprocess.Popen started in its own session or process group
popen_group_kwargs(no_window=False) the Popen keyword arguments that popen_group adds

Options for run

Option Default Meaning
cwd, env inherited Working directory and environment of the command
input none Bytes written to stdin. Without it stdin is closed
timeout none Seconds before the run is stopped with stop_reason == "timeout"
max_output 10_000_000 Bytes kept from the start of each stream. None keeps everything
stop_on_output_limit True Stop the command when a stream passes max_output. False keeps reading and drops the excess
tail_bytes 8192 Bytes kept from the end of each stream, even past max_output
grace 5.0 Seconds between the soft signal and the forced kill
cancel none A threading.Event. Setting it stops the run with stop_reason == "cancelled"
on_output none Called as on_output("stdout" or "stderr", chunk) from reader threads. Exceptions it raises are ignored
kill_process_tree True False signals only the command itself and leaves its descendants running
drain_timeout 1.0 Seconds to wait for the pipes to close after the command exits. If a descendant still holds them, the tree is killed
no_window False Windows only: start the command without a console window
check False Raise CommandError unless the command exits with code 0

RunResult

Field Meaning
argv The command that ran
exit_code Exit code. None if it never started. Negative signal number after a kill on macOS and Linux
stop_reason "exited", "timeout", "cancelled", "output-limit" or "spawn-failed"
timed_out, cancelled, ok Shortcuts. ok means it exited by itself with code 0
truncated A stream produced more than max_output bytes
duration Seconds from start to finish
stdout, stderr Captured bytes, at most max_output each. stdout_text and stderr_text decode them
stdout_tail, stderr_tail The last tail_bytes of each stream. error_tail(limit) returns the stderr tail as text, or the stdout tail if stderr is empty
pid Process id of the command
kill A KillInfo when the run was stopped, otherwise None
error The operating system's message when stop_reason == "spawn-failed"

KillInfo has method, already_exited, graceful (the soft signal was enough) and forced (SIGKILL or taskkill /F was needed).

Notes

  • kill_tree on macOS and Linux needs the target to lead its own process group. Processes started by run and popen_group do. It raises ValueError rather than signal the calling process's own group.
  • On Windows the soft step is CTRL_BREAK_EVENT, which ends most console programs. Descendants of a command that exits during the grace period cannot be found afterwards and keep running.
  • Error messages from check=True name the program but not its arguments.

License

MIT

Metadata

Release files for uppercut-subprocess-runner 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for uppercut-subprocess-runner 0.1.0
File Size Uploaded Provenance Actions
uppercut_subprocess_runner-0.1.0.tar.gz 10.3 kB Without provenance Details for uppercut_subprocess_runner-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for uppercut-subprocess-runner 0.1.0
File Interpreter ABI Platform Actions
uppercut_subprocess_runner-0.1.0-py3-none-any.whl Python 3 none any Details for uppercut_subprocess_runner-0.1.0-py3-none-any.whl

Total release size: 19.5 kB

Release files / uppercut_subprocess_runner-0.1.0.tar.gz

Download URL uppercut_subprocess_runner-0.1.0.tar.gz
Size 10.3 kB
Tags Source
SHA-256 checksum
How to use checksums
fea4800aab9afb5369e38d0d6003ab6582ec1aeaecdc2125ea6211b7cc339d3e
BLAKE2b-256 checksum
How to use checksums
e271e4cf0b0b309ed28120471f65081225d712d33dc99fee4fbfbd2de03c0aa4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / uppercut_subprocess_runner-0.1.0-py3-none-any.whl

Download URL uppercut_subprocess_runner-0.1.0-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b0dc9a443509ce1c89b373039cc38f30fe9f66204886710958abc3fcb3d59562
BLAKE2b-256 checksum
How to use checksums
9d8fa75843cdc796a90ae4265a6a321c8e1bbf276c9102630f89dfb38caa1d78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for uppercut-subprocess-runner 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page