Skip to content

Why proclet โ€‹

  • We needed a way to run external tools from Python code without repeating subprocess boilerplate.
  • Redirecting output to a logger, feeding stdin from a queue or chaining binary-safe pipelines shouldn't require hand-written threads.
  • The same API should serve sync and asyncio code.
  • The library should stay framework-agnostic: no runtime dependencies, no logging setup, failures are exceptions the caller decides about.

Streaming a database dump through a compressor, logging both tools' stderr line by line, with a timeout and an exit code check, looks like this with subprocess:

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:

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)
)

Compared to other libraries โ€‹

subprocessshplumbuminvokeproclet
Stylefunction argumentscommands as functionslocal["git"] objects and operatorsshell command stringschainable builder
Windowsyesnoyesyesyes
asyncioseparate API_async=Trueseparate async commandsnosame Command (aexec())
Pipelines without a shellhand-wired Popensyesyesnoyes, raw OS pipes
Run as another useruser=_uid=nonouser()
Choose the group and supplementary groupsgroup=, extra_groups=nononogroup(), extra_groups()
Lower the priorityby hand (preexec_fn)nonononice(), Windows included
  • Checked against sh 2.4, plumbum 2.0 and invoke 3.0. invoke is mainly a task runner and plumbum also covers CLI apps and remote machines: pick them for that.
  • Spotted a mistake? Please open an issue.
  • Already using one of them? See Coming from sh and Coming from plumbum.

Released under the Apache-2.0 license.