Skip to main content

Proclet

Proclet is a fluent, typed wrapper around Python's subprocess, with matching sync and async APIs, binary-safe pipelines, privilege dropping and low-priority execution.

pypi version python versions continuous integration no runtime dependencies license

📦 Prerequisites

  • Python 3.12 or newer, on Linux, macOS or Windows.
  • No runtime dependency.
  • uv to work on the project itself.

✨ Features

  • Ability to configure and run a command through a chainable, fully typed API.
  • Ability to run the same command synchronously or with asyncio.
  • Ability to redirect output to files, file objects, StringIO/BytesIO or a callable receiving each line.
  • Ability to feed stdin from a string, bytes, a file object or a queue.
  • Ability to chain commands into pipelines connected with raw OS pipes, safe for binary payloads.
  • Ability to enforce timeouts and the exit codes that count as success.
  • Ability to build the environment and PATH of a command from the current one.
  • Ability to drop privileges to another user, group and set of supplementary groups.
  • Ability to run a command with a lower CPU (and, on Linux, IO) priority.
  • Ability to wrap a binary into a typed API of its own.
  • Ability to observe every process spawn and exit.

🎯 Motivation

  • 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:

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:

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

subprocess sh plumbum invoke proclet
Style function arguments commands as functions local["git"] objects and operators shell command strings chainable builder
Windows yes no yes yes yes
asyncio separate API _async=True separate async commands no same Command (aexec())
Pipelines without a shell hand-wired Popens yes yes no yes, raw OS pipes
Run as another user user= _uid= no no user()
Choose the group and supplementary groups group=, extra_groups= no no no group(), extra_groups()
Lower the priority by hand (preexec_fn) no no no nice(), 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.

🔨 Workflow

Setup

The following steps will ensure your project is cloned properly.

  1. Clone repository:
    git clone https://github.com/techcode-io/proclet
    cd proclet
    
  2. Install dependencies and setup environment:
    uv sync
    uv run poe env:configure
    

Lint

  • To lint you have to use the workflow.
uv run poe lint
  • It will lint the project code using ruff.

Format

  • To format you have to use the workflow.
uv run poe fmt
  • It will format the project code using ruff, including the Python examples of this README.

Test

  • To test you have to use the workflow.
uv run poe test
  • It will run the test suite using pytest.
  • uv run poe ci runs both lint and tests, as the CI does.

📖 Usage

How to install

pip install proclet

How to get started

import asyncio
from io import StringIO
from proclet import Command

# Run a command and capture its output.
out = StringIO()
Command("git").args("rev-parse", "HEAD").stdout(out).timeout(10).exec()
print(out.getvalue().strip())

# Chain commands through raw OS pipes, no shell involved.
Command("git").args("log", "--format=%an").pipe(
    Command("sort").args("-u").inherit_out()
)

# Run the same configuration with asyncio.
asyncio.run(Command("git").args("fetch", "--all").inherit_out().aexec())
  • A failing command raises ErrorReturnCode, a missing binary CommandNotFound and an elapsed timeout TimeoutException.
  • The following sections cover each feature in detail.

How to run a command

from io import StringIO
from proclet import Command

out = StringIO()
Command("git").args("status", "--short").stdout(out).exec()
print(out.getvalue())
  • A command is configured with chainable setters, then run with exec(). Called without a value, most setters return the current one: cmd.args(), cmd.env(), cmd.cwd()...
  • exec() waits for completion and returns the finished subprocess.Popen.
  • Unless redirected, stdin reads from the null device and the output is discarded.

How to handle failures

from proclet import Command, CommandNotFound, ErrorReturnCode, TimeoutException

try:
    plan = (
        Command("terraform")
        .args("plan", "-detailed-exitcode")
        .ok_exit_codes(0, 2)  # 2 means "changes pending", not a failure
        .timeout(300)
        .inherit()
        .exec()
    )
    print("changes pending" if plan.returncode == 2 else "up to date")
except CommandNotFound as err:
    print(f"{err.command} is not installed")
except ErrorReturnCode as err:
    print(f"{err.cmd} failed with exit code {err.exit_code}")
except TimeoutException as err:
    print(f"{err.cmd} took more than {err.timeout}s and was terminated")
  • CommandNotFound is raised at construction for a name missing from PATH; a Path is only checked when the command runs.
  • ErrorReturnCode is raised for an exit code outside ok_exit_codes(...) (default: only 0). A process killed by a signal reports a negative exit code.
  • TimeoutException is raised once timeout(...) elapses; the process is terminated first.
  • proclet never logs, prints or exits on its own: the caller decides what a failure means.

How to capture and stream output

import logging
from io import BytesIO, StringIO
from pathlib import Path
from proclet import Command, strip_redirect

log = logging.getLogger(__name__)

text = StringIO()
Command("git").args("log", "-1", "--format=%H").stdout(text).exec()

raw = BytesIO()
Command("git").args("show", "HEAD:logo.png").stdout(raw).exec()

Command("make").args("build").stdout(Path("build.log")).stderr(
    strip_redirect(log.warning)
).exec()
  • stdout(...) and stderr(...) accept a Path (truncated, then written), a file object (text or binary, including StringIO/BytesIO) or a callable.
  • A callable receives each decoded line as it is produced, newline included: strip_redirect(...) wraps one to drop it, which suits loggers.
  • inherit() wires stdin, stdout and stderr to the current process; inherit_out() only stdout and stderr, and inherit_out(if_unset=True) only the ones not redirected yet.

How to feed stdin

import threading
from queue import Queue
from proclet import Command

Command("sort").stdin("pear\napple\n").inherit_out().exec()

with open("data.csv", "rb") as source:
    Command("wc").args("-l").stdin(source).inherit_out().exec()

lines: Queue[bytes | str | None] = Queue()


def produce() -> None:
    for number in range(3):
        lines.put(f"event {number}\n")
    lines.put(None)  # closes the command's stdin


threading.Thread(target=produce).start()
Command("cat").stdin(lines).inherit_out().exec()
  • stdin(...) accepts a str (encoded as UTF-8) or bytes payload, or a file object: one with a real file descriptor is handed to the child as is, others (StringIO/BytesIO) are read and fed.
  • A queue streams items as they come and closes stdin when it yields None: a queue.Queue with exec(), an asyncio.Queue with aexec().

How to run a command in the background

from proclet import Command

server = Command("python").args("-m", "http.server", "8000").exec(wait_completion=False)
try:
    ...  # talk to the server
finally:
    server.terminate()
    server.wait()
  • exec(wait_completion=False) returns the running subprocess.Popen as soon as it is spawned.
  • timeout(...) and ok_exit_codes(...) aren't enforced then; redirect files are still closed once the process exits.

How to run commands with asyncio

import asyncio
from io import StringIO
from proclet import Command


async def uptimes(hosts: list[str]) -> dict[str, str]:
    outputs = {host: StringIO() for host in hosts}
    await asyncio.gather(
        *(
            Command("ssh").args(host, "uptime").stdout(out).timeout(10).aexec()
            for host, out in outputs.items()
        )
    )
    return {host: out.getvalue().strip() for host, out in outputs.items()}


print(asyncio.run(uptimes(["alpha", "beta", "gamma"])))
  • Every way of running a command has an asyncio counterpart prefixed with a: aexec(), apipe() and apipeline(). They return an asyncio.subprocess.Process.
  • The configuration is shared: the same Command runs with either.

How to chain commands into a pipeline

from proclet import Command

Command("tar").args("-c", "data").pipeline(
    Command("zstd").args("-q"),
    Command("tee").args("data.tar.zst"),
)

Command("git").args("log", "--format=%an").pipe(
    Command("sort").args("-u").inherit_out()
)
# or: await Command(...).apipeline(...) / .apipe(...)
  • pipe(other) connects two commands, pipeline(*others) any number of them; all stages run concurrently.
  • Stages are connected with raw OS pipes, so binary payloads aren't decoded on the way.
  • The first stage keeps its stdin, the last one its stdout; each stage keeps its own stderr, env, cwd, timeout and exit codes.
  • Stages are checked in order: the first failure raises and the stages still running are terminated. The last stage's process is returned.

How to set the environment and PATH

from pathlib import Path
from proclet import Command, EnvCmd, PathCmd

env = (
    EnvCmd.inherit(lambda key, _: key.startswith("LC_"))
    .add({"PATH": PathCmd.inherit().add("/opt/tools/bin").build()})
    .build()
)
Command("deploy").env(env).cwd(Path("infra")).exec()

Command("python", search_path=["/opt/python3.13/bin"]).args("--version").exec()
  • Without env(...) a command inherits the current environment; once set, it gets exactly that mapping, so build on EnvCmd.inherit() to keep it.
  • EnvCmd.inherit(predicate) keeps only the variables predicate(key, value) accepts; EnvCmd.empty() starts from nothing.
  • PathCmd.add(...) prepends a directory, so it takes precedence over the inherited PATH.
  • search_path looks the binary up in the given directories instead of PATH.

How to drop privileges and lower priority

from proclet import Command

(
    Command("rsync")
    .args("-a", "src/", "dst/")
    .user("backup")  # requires sufficient privileges, e.g. root
    .group("backup")
    .extra_groups([])  # drop supplementary groups
    .nice(19)  # lowest priority
    .exec()
)
  • user, group and extra_groups accept names or ids. They are POSIX only and raise ValueError elsewhere.
  • extra_groups unset keeps the current supplementary groups; [] drops them all.
  • nice(...) lowers the CPU priority; on Linux, nice(19) also puts the process in the idle IO class. On Windows the niceness maps to a priority class.

How to reuse a configuration

from pathlib import Path
from proclet import Command

base = Command("git").cwd(Path("repo")).timeout(30)
Command("git").copy(base).args("fetch", "--all").exec()
Command("git").copy(base).args("gc").exec()
  • copy(other) takes every setting of other (args, env, cwd, redirects, timeout, exit codes, privileges, niceness, observers) while keeping its own binary.

How to wrap a binary into a typed API

from io import StringIO
from pathlib import Path
from proclet import Bind, Command


class Git(Bind):
    def __init__(self, repo: Path) -> None:
        super().__init__("git")
        self.cwd(repo)

    def log(self, *refs: str) -> Command:
        return self._command("log", "--oneline", *refs)

    def current_branch(self) -> str:
        out = StringIO()
        self._command("branch", "--show-current").stdout(out).exec()
        return out.getvalue().strip()


git = Git(Path("repo")).nice(10)
print(git.current_branch())
git.log("main").inherit_out().timeout(30).exec()
git.log("main", "--", "README.md").pipe(Command("wc").args("-l").inherit_out())
  • A Bind holds the context shared by every operation on its binary: env, cwd, user, group, extra_groups, nice and observers.
  • Each operation calls self._command(...), which returns a fresh Command carrying that context, then either returns it, for the caller to redirect, run or pipe (log), or runs it and returns a result (current_branch).
  • Settings made on one operation's command never leak into another one.
  • Set default context in the constructor, and share per-operation defaults through a private helper:
from proclet import Bind, Command, EnvCmd


class Terraform(Bind):
    def __init__(self) -> None:
        super().__init__("terraform")
        self.env(EnvCmd.inherit().add({"TF_IN_AUTOMATION": "1"}).build())

    def _run(self, *args: str) -> Command:
        return self._command(*args).timeout(600)

    def plan(self) -> bool:
        """Whether changes are pending."""
        plan = self._run("plan", "-detailed-exitcode").ok_exit_codes(0, 2).exec()
        return plan.returncode == 2
  • A caller's env(...) replaces the bind's env: merge onto bind.env() to keep its defaults.

How to observe processes

import time
from proclet import Command, Process


class Timer:
    def __init__(self) -> None:
        self.started: dict[int, float] = {}

    def on_spawn(self, process: Process) -> None:
        self.started[process.pid] = time.monotonic()

    def on_exit(self, process: Process) -> None:
        elapsed = time.monotonic() - self.started.pop(process.pid)
        print(f"pid {process.pid} exited with {process.returncode} in {elapsed:.2f}s")


Command("git").args("fetch").add_observer(Timer()).exec()
  • add_observer(...) registers any object implementing the ProcessObserver protocol, notified with on_spawn(process) and on_exit(process); Bind.add_observer(...) watches every operation.
  • Hooks run synchronously and must not raise. After exec(wait_completion=False), on_exit runs on a background thread.

❤️ Contributing

If you find this project useful here's how you can help, please click the 👁️ Watch button to avoid missing notifications about new versions, and give it a 🌟 GitHub Star!

You can also contribute by:

  • Sending a Pull Request with your awesome new features and bug fixed.
  • Be part of the community and help resolve Issues.

Before opening a pull request:

  • Run uv run poe ci: it lints and tests the project, as the CI does on Linux, macOS and Windows.
  • Write tests with the helpers of tests/portable.py (py(...), cat(), echo()...) rather than POSIX tools such as sh or cat, so they run on every platform.
  • Keep proclet free of runtime dependencies.
  • Start commit titles with build, ci, docs, feat, fix, perf, refactor, test, chore or release, followed by a colon: gitlint checks it.
  • Add user-facing changes to the ## [Unreleased] section of CHANGELOG.md.

🧾 License

The proclet project is free and open-source software licensed under the Apache-2.0 license.

Metadata

Release files for proclet 0.1.1

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

Source distribution (sdist)

Source distribution for proclet 0.1.1
File Size Uploaded Provenance Actions
proclet-0.1.1.tar.gz 33.9 kB With provenance Details for proclet-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for proclet 0.1.1
File Interpreter ABI Platform Actions
proclet-0.1.1-py3-none-any.whl Python 3 none any Details for proclet-0.1.1-py3-none-any.whl

Total release size: 65.0 kB

Release files / proclet-0.1.1.tar.gz

Download URL proclet-0.1.1.tar.gz
Size 33.9 kB
Tags Source
SHA-256 checksum
How to use checksums
eb0aa89ade9790b062e4522f7928524548aedcd6b48167e6b07a4ee390d32885
BLAKE2b-256 checksum
How to use checksums
7b11398e6d3ff91b79342e7d6661086b6f7c2eee1002ad1ac97c9c62c800d54f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.

Transparency log

Release files / proclet-0.1.1-py3-none-any.whl

Download URL proclet-0.1.1-py3-none-any.whl
Size 31.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8fb3266805a3dc02d3d52ff86f4e3209b24ca555ae5e970c16654a2b39f894e7
BLAKE2b-256 checksum
How to use checksums
9a54724ebde8d6b9bf04ca938da4f1f3d3074fd07dd2ab981033b80d365fe78a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.

Transparency log

Security for proclet 0.1.1

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for proclet-0.1.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.

Transparency log

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.

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