Skip to main content

ptymini

ptymini provides asyncio-native pty sessions (ptymini.core) and a synchronous background-terminal API (ptymini.bg) backed by a compiled Rust core.

A PtySession spawns one pty that multiple clients can attach to as independent byte streams, each primed from a byte-bounded Ring replay buffer so reconnecting clients resume with recent scrollback instead of a blank screen. PtyRegistry adds named get-or-create sessions, injected shell setup (rc), and inactivity culling without adding a server, framework, or cross-process persistence. Web exposure belongs to the embedding app (such as jupygate), and durable session management belongs to tmux.

ptymini.bg exposes the bgterm API for callers outside an event loop, such as scripts, kernel tools, and LLM agents. Calls block in the Rust core with the GIL released and share fastmux.bg's wait semantics (wait_ms, until, and settle_ms).

Install

pip install ptymini

Asyncio sessions (ptymini.core)

PtySession supports two ways to read from the same bounded Ring buffer:

  • attach() returns an async iterator that replays retained scrollback, yields new bytes as they arrive, and ends at EOF. Each attachment tracks its own position, and stopping an iterator leaves the terminal and other readers untouched. With gaps=True, a reader that fell behind receives a Gap(n) count of lost bytes before resuming at the oldest retained byte.
  • read_from(offset, max_bytes_out=None) and wait_change(seen_end=offset, timeout=...) read pages from an explicit byte offset without advancing any other reader.
import asyncio
from ptymini.core import PtySession

async def main():
    async with PtySession(["bash", "--norc", "--noprofile", "-i"]) as t:
        t.write(b"echo hi $((6*7))\n")
        async for chunk in t.attach():
            if b"hi 42" in chunk: break
        t.resize(40, 100)
        await t.terminate(force=True)
        print("exit:", await t.wait())

asyncio.run(main())

terminate() sends SIGHUP, SIGCONT, SIGINT, and SIGTERM in order until the child exits (following terminado's PtyWithClients.terminate, BSD-licensed, Copyright (c) Jupyter Development Team). With force=True, it sends SIGKILL if the child still has not exited. Exiting an async with block calls terminate(force=True).

PtyRegistry manages named sessions. create(name=...) returns an existing session or spawns a new one, auto-numbering when name is omitted. env replaces the inherited environment, appendenv overrides keys in it, and rc writes shell setup to a private .zshrc directory and substitutes {rcfile} and {rcdir} in argv and environment values (use --rcfile {rcfile} for Bash or ZDOTDIR={rcdir} for Zsh). Set cull_timeout and call cull_ready(), cull(), or run cull_loop(reg) to terminate inactive sessions.

from ptymini.core import PtyRegistry

async with PtyRegistry(argv=["bash", "--norc", "--noprofile", "-i"]) as terms:
    t = await terms.create(name="scratch")
    tr = await terms.create(
        argv=["bash", "--noprofile", "--rcfile", "{rcfile}", "-i"],
        rc="PS1='$ '\nalias hi='echo rc-worked'",
    )

Synchronous background sessions (ptymini.bg)

start_bgterm spawns a child process and returns an integer session ID. String commands run through /bin/sh -c with TERM=dumb by default (pass term="xterm-256color" to test full terminal output). poll, write_stdin, and read advance the session cursor and return a PollResult whose remaining_bytes, dropped_bytes, and truncated fields report unread backlog and buffer overflow.

  • wait_ms bounds the wait for new output or an until regex match.
  • until matches unread text only (never text already returned by an earlier read).
  • settle_ms waits for output to go quiet after bursts arrive; total wait never exceeds wait_ms + settle_ms, and process exit ends any wait immediately.
from ptymini.bg import Session, close_bgterm, poll, start_bgterm, write_stdin

sid = start_bgterm(["ipython", "--simple-prompt"])
poll(sid, 5000, until=r"In \[1\]")
res = write_stdin(sid, "2+2\n", 5000, until=r"In \[2\]")
close_bgterm(sid)

with Session.start(["python", "-c", "print('bye'); raise SystemExit(3)"]) as sess:
    print(sess.poll(3000).text)
    assert sess.wait(3000) == 3

Tests

pytest -q

Metadata

Release files for ptymini 0.0.10

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

Source distribution (sdist)

Source distribution for ptymini 0.0.10
File Size Uploaded Provenance Actions
ptymini-0.0.10.tar.gz 24.7 kB With provenance Details for ptymini-0.0.10.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for ptymini 0.0.10
File Interpreter ABI Platform Actions
ptymini-0.0.10-cp311-abi3-manylinux_2_31_x86_64.whl CPython 3.11 abi3 Linux glibc 2.31+ x86-64 Details for ptymini-0.0.10-cp311-abi3-manylinux_2_31_x86_64.whl
ptymini-0.0.10-cp311-abi3-manylinux_2_31_aarch64.whl CPython 3.11 abi3 Linux glibc 2.31+ ARM64 Details for ptymini-0.0.10-cp311-abi3-manylinux_2_31_aarch64.whl
ptymini-0.0.10-cp311-abi3-macosx_11_0_arm64.whl CPython 3.11 abi3 macOS 11.0+ ARM64 Details for ptymini-0.0.10-cp311-abi3-macosx_11_0_arm64.whl

Total release size: 949.6 kB

Release files / ptymini-0.0.10.tar.gz

Download URL ptymini-0.0.10.tar.gz
Size 24.7 kB
Tags Source
SHA-256 checksum
How to use checksums
084ba803a67342133151b56493d9df606582be10d8ddd23816bd5aa6584fd587
BLAKE2b-256 checksum
How to use checksums
47563f73ef82accf951093c83afbcc37da59bfba5f9bf7ed21f06b6c30b322b4
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 Dec 2, 2026.

Transparency log

Release files / ptymini-0.0.10-cp311-abi3-manylinux_2_31_x86_64.whl

Download URL ptymini-0.0.10-cp311-abi3-manylinux_2_31_x86_64.whl
Size 312.0 kB
Tags CPython 3.11 Linux glibc 2.31+ x86-64 abi3
SHA-256 checksum
How to use checksums
4663d59fa3caef6d9a778a7f1773360668acf78db3e40ccf53196cc2878fa691
BLAKE2b-256 checksum
How to use checksums
94c2f441a3ab31f5ea0df3c79380874bbefdf0582e22f005c99bcf8f55ab94c7
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 Dec 2, 2026.

Transparency log

Release files / ptymini-0.0.10-cp311-abi3-manylinux_2_31_aarch64.whl

Download URL ptymini-0.0.10-cp311-abi3-manylinux_2_31_aarch64.whl
Size 302.7 kB
Tags CPython 3.11 Linux glibc 2.31+ ARM64 abi3
SHA-256 checksum
How to use checksums
5340b883d7c1cca9f18f8c230d86c08e4fddff8dec6ae43fbc9af9392794f354
BLAKE2b-256 checksum
How to use checksums
b42cd39070102de6eddd6674f6e9a2e513344deac764bd9ebf3d75699257e842
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 Dec 2, 2026.

Transparency log

Release files / ptymini-0.0.10-cp311-abi3-macosx_11_0_arm64.whl

Download URL ptymini-0.0.10-cp311-abi3-macosx_11_0_arm64.whl
Size 310.3 kB
Tags CPython 3.11 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
faf0e4db8c60ed2bc555c479e514260909aed605f168cab73fbc374a63fd6359
BLAKE2b-256 checksum
How to use checksums
ee1a16e89c94848bed58d27dbdca3814f1b3b26989f7e40654e2fbf41de2773b
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 Dec 2, 2026.

Transparency log

Security for ptymini 0.0.10

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

All 4 files in this release have been uploaded with provenance metadata.

Select from 4 files

Attestation for ptymini-0.0.10.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 Dec 2, 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