Skip to content

procletSubprocess, without the boilerplate

Run external commands from Python through one fluent, typed API. The same command runs with exec() or await aexec(), chains into shell-free pipelines, and streams its output wherever you need it.

pip install proclet
pg_dump pipes its output into zstd, which writes shop.sql.zst; both send their stderr, line by line, to logging.warning. stderrstderrpg_dumpzstdshop.sql.zstlogging.warning

The same job, twice โ€‹

Stream a database dump through a compressor, log both tools' stderr line by line, enforce a timeout and check every exit code.

With subprocess 35 lines

python
import logging
import subprocess
import threading

log = logging.getLogger(__name__)

dump = subprocess.Popen(
    ["pg_dump", "shop"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
pack = subprocess.Popen(
    ["zstd", "-q", "-o", "shop.sql.zst"], stdin=dump.stdout, stderr=subprocess.PIPE
)
dump.stdout.close()  # let pg_dump see a broken pipe if zstd dies


def forward(stream):
    for line in stream:
        log.warning(line.decode().rstrip("\n"))


threads = [threading.Thread(target=forward, args=(p.stderr,)) for p in (dump, pack)]
for thread in threads:
    thread.start()
try:
    dump.wait(timeout=600)
    pack.wait(timeout=600)
finally:
    for process in (dump, pack):
        if process.poll() is None:
            process.kill()
    for thread in threads:
        thread.join()
for process in (dump, pack):
    if process.returncode != 0:
        raise RuntimeError(f"{process.args} exited with {process.returncode}")

With proclet 9 lines

python
import logging
from proclet import Command, strip_redirect

log = logging.getLogger(__name__)
to_log = strip_redirect(log.warning)

Command("pg_dump").args("shop").stderr(to_log).timeout(600).pipe(
    Command("zstd").args("-q", "-o", "shop.sql.zst").stderr(to_log).timeout(600)
)

What it handles for you โ€‹

Sync and async
One Command, run with exec() or await aexec(). Async guide
Pipelines without a shell
Stages joined by raw OS pipes: no quoting, no injection, binary data untouched. Binary pipes
Output where you want it
Send each line to a logger as it is written, or to a file, a StringIO or a BytesIO. Stream to a logger
Input from anything
Feed stdin from a string, bytes, a file or a queue that streams while the command runs. Input and output
Timeouts that clean up
Terminate, wait, then kill, and raise TimeoutException, in pipelines too. Timeouts
Privileges and priority
Run as another user and group, drop supplementary groups, lower the CPU and IO priority. Privileges and priority
Typed wrappers for your tools
Turn git, kubectl or ffmpeg into a Python class with Bind. Wrap a CLI
Nothing else to install
Standard library only, on Linux, macOS and Windows, Python 3.12 or newer. Getting started

Already using sh or plumbum? โ€‹

The migration guides map what you know to proclet, side by side: coming from sh and coming from plumbum.

Released under the Apache-2.0 license.