zppy
zppy is a Python library that reports task progress through the Zig progress
protocol. Use it to report tasks, subtasks, and progress from child processes.
zppy requires Python 3.11 or later. The library has no runtime dependencies.
The pipe transport supports POSIX systems, including Linux and macOS.
Installation
python -m pip install zppy
The package uses the CC0 1.0 Universal license.
Zig progress protocol
The Zig progress protocol lets a process send a tree of tasks to its parent
through a pipe. Each task has a name, a completed item count, and an estimated
total. The ZIG_PROGRESS environment variable identifies the pipe. The parent
can combine progress from child processes and display it in one place.
You can use the protocol outside the Zig toolchain. For example,
zpg runs commands and displays their
progress in a terminal. With zppy, Python programs can send progress to
zpg or another compatible receiver.
See Andrew Kelley's Zig progress protocol specification for the protocol design.
Library usage
If you use this library, open an issue or send a pull request with a link to your project. This helps us track use of the Zig progress protocol outside the Zig toolchain.
Add the zig-progress-protocol topic to your GitHub repository. Use
the same tag on other platforms so people can find related projects.
Report tasks
Save this example as build.py:
import time
from zppy import Progress
with Progress("build", estimated_total_items=2) as progress:
for name in ("assets", "code"):
with progress.root.start(name, estimated_total_items=10) as task:
for _ in range(10):
time.sleep(0.1) # Replace this with one item of work.
task.complete_one()
Run build.py with a progress display:
zpg python build.py
Each child task can start more child tasks. When a child task ends, it leaves
the tree and adds one completed item to its parent task. Do not also call
parent.complete_one() for the same child task.
A context manager ends its task on normal exit and on exceptions. The context manager does not suppress exceptions or report task success or failure.
Without a valid ZIG_PROGRESS channel, reporting is disabled. Task operations
have no effect. The library does not print to stdout or stderr. It does not
draw its own terminal display.
Run the combined example
From the repository root, run:
PYTHONPATH=src zpg python -u examples/mock.py
This example requires zpg on PATH and a system with os.fork(). It runs
zpg --mock as a subprocess and a Python worker as a forked process at the
same time. Both task trees appear under demo. Python task labels start with
PY. Both workers also write messages to stdout and stderr.
Child processes
Use Node.spawn() to start a process with its own progress pipe.
Node.spawn() accepts the arguments to subprocess.Popen and returns a
Popen object:
import sys
from zppy import Progress
with Progress("build", estimated_total_items=1) as progress:
with progress.root.start("compile") as task:
with task.spawn([sys.executable, "worker.py"]) as process:
returncode = process.wait()
In worker.py, use Progress. The parent process combines the child's task
tree with its own tasks and sends the combined tree upstream. A child process
can use Node.spawn() to start more processes. Use a separate task node for
each process. Processes on different nodes can run at the same time.
Node.spawn() follows Zig's child progress behavior. The child's
root replaces the task node in each report. The child root's descendants
appear below that node. If the child root has no name, the task keeps its
local name. Until the first complete report arrives, the task keeps its local
state. The last complete tree stays visible after the child closes its pipe or
exits.
Wait for the process, then end its task, as in the example above. Process exit does not end the task or mark success or failure. Ending the task completes one item in its parent.
Ending a task or closing the reporter closes its child progress pipes. Neither
operation waits for or terminates processes. Use one process per active node.
A second spawn() or fork() on the same node raises ValueError.
The update thread collects child reports. With refresh_rate=None, call
flush() regularly while children run.
spawn() copies the child environment and sets ZIG_PROGRESS to the new
pipe. It preserves other environment entries and adds the pipe to pass_fds.
close_fds must be true. spawn() does not change the parent environment.
Disabled, ended, or closed nodes still start processes, but remove
ZIG_PROGRESS from the child environment. Do not pass the upstream progress
descriptor to children.
All processes in the tree must use the same name_size. There is no format
negotiation.
Forked processes
On systems with os.fork(), use Node.fork() to attach a forked process to a
task. It returns the child's PID in the parent and 0 in the child:
import os
from zppy import Progress
with Progress("parent", estimated_total_items=1) as progress:
with progress.root.start("worker") as task:
pid = task.fork()
if pid == 0:
with Progress("worker", estimated_total_items=10) as child:
for _ in range(10):
child.root.complete_one()
os._exit(0)
_, status = os.waitpid(pid, 0)
The child gets a private ZIG_PROGRESS pipe. Create a new Progress in the
child, with the same name_size as its parent. All inherited reporters and
their nodes are disabled in the child. Caller-owned descriptors remain the
caller's responsibility. The parent's reporters and environment stay
unchanged.
The forked child's tree replaces its task node, as for spawn(). Forked
children can use either fork() or spawn() to create more children. Wait
for the child with os.waitpid(), then end its task in the parent. Close the
child's reporter before os._exit(), which skips normal cleanup. Handle child
errors and exit status as required by your application.
Forks on disabled, ended, or closed nodes still run, but the child has no
ZIG_PROGRESS channel. A failed fork leaves the parent reporter usable.
Use Node.fork() to connect a forked process to a task. Direct os.fork()
calls and multiprocessing do not create this connection. Node.fork()
protects zppy's own state. Application code and other libraries must also
meet the platform's fork
requirements.
API reference
Progress(root_name="", estimated_total_items=0, *, refresh_rate=0.08, initial_delay=0.2, fd=None, disable=False, name_size=120) creates a reporter.
Use progress.root to access its root task.
| Option | Meaning |
|---|---|
root_name |
Root label, as str or bytes. |
estimated_total_items |
Estimated root item count. Zero means unknown. |
refresh_rate |
Seconds between reports. Use None for manual reports. |
initial_delay |
Seconds before the first automatic report. |
fd |
Explicit pipe descriptor. None reads ZIG_PROGRESS. |
disable |
Disable all reporting when true. |
name_size |
Name field size: 120 by default, or 40 for the old format. |
Timer values must be finite numbers no greater than threading.TIMEOUT_MAX.
refresh_rate must be positive, and initial_delay must be nonnegative.
progress.enabled shows whether the channel is open. progress.flush() tries
to send a snapshot immediately. It returns True if the current snapshot was
sent in full, or False if reporting is disabled or the write could not
finish. It does not wait for pipe space.
progress.close() stops the update thread, tries to send the final state, and
releases its descriptor. Use a context manager or call close() directly.
Closing the reporter does not set counts to their totals or remove the final
tree from the receiver. Ending the root also closes the reporter.
Nodes provide these methods:
| Method | Action |
|---|---|
start(name, estimated_total_items=0) |
Create a child task. |
spawn(args, **kwargs) |
Start a process and collect its progress. |
fork() |
Fork and collect the child's progress at this node. |
complete_one() |
Add one completed item. |
set_completed_items(count) |
Set the absolute completed count. |
set_estimated_total_items(count) |
Set the estimated total. |
increase_estimated_total_items(count) |
Add to the estimated total. |
set_name(name) |
Change the task label. |
end() |
Remove this branch and complete one item in its parent. |
Calls are thread-safe. Repeated end() calls have no effect. Task updates on
ended nodes also have no effect. Calls to spawn() and fork() still start a
process on ended nodes, as described in the child process sections. Invalid
arguments raise TypeError or ValueError, including when reporting is
disabled.
Counts must be nonnegative integers. Completed counts are limited to
4294967295. Totals are limited to 4294967294, because Zig reserves the
next value. The library reduces larger values to these limits. Completed
counts can exceed totals.
Names are zero-padded to the field size. Text is encoded as UTF-8 and shortened at a character boundary. Byte names are shortened without decoding. Bytes after the first NUL are ignored.
The reporter tracks up to 253 active nodes, including the root. Additional nodes have no effect. Ending nodes makes space for new nodes. Combined reports also have a 253-node limit. Local nodes take priority, followed by child trees in process creation order. Child roots replace local nodes without using more slots. The reporter omits excess descendants and their branches. These descendants can appear in later reports when space is available.
Technical details
Protocol compatibility
The default uses a 120-byte name field. Each node has two little-endian 32-bit
counters and 120 name bytes. A packet contains the node count, all node
records, then all parent indices. The root's parent is 255.
The published specification uses 40-byte names. Select that format
explicitly with Progress(name_size=40). The two formats are not wire
compatible. Both use ZIG_PROGRESS, and there is no version handshake. Select
the format that matches your receiver.
Use one reporter per pipe. Each packet replaces the previous tree. A packet
does not add tasks to the previous tree. Multiple writers can replace each
other's progress or interleave packet bytes. Use Node.spawn() or
Node.fork() to combine progress from separate processes through private
pipes. Create a new reporter in each child, and use the same name_size
throughout the process tree.
Platforms and pipe handling
The reporter accepts only writable POSIX pipes, such as those on Linux and macOS. Other descriptors, including regular files and read-only pipes, disable reporting without changing their data or blocking mode. Reporting is disabled on other platforms.
The reporter duplicates the supplied descriptor and never closes the original.
The duplicate is not inherited across exec. The reporter sets the channel to
nonblocking mode. This flag also applies to the original descriptor. When you
create a reporter, it does not change ZIG_PROGRESS in the environment.
Node.fork() sets ZIG_PROGRESS in the forked child only.
A full pipe can delay or drop updates. The reporter resumes partial packets before it sends a new snapshot. A permanent write error disables reporting.
Closing the reporter never waits for pipe space. The final report can be lost.
If the receiver stops reading, a large partial packet can remain incomplete.
If final delivery is required, retry flush() while the receiver is active
before you close the reporter.
Release notes
0.1.0
Initial release:
- Report tasks and nested tasks through the Zig progress protocol.
- Combine progress from subprocesses and forked Python workers.
- Support both 120-byte and 40-byte task names.
- Send updates through POSIX pipes without runtime dependencies.
Manual releases
Use the Nix development shell on this machine:
nix develop
The shell includes the build tools and Twine. Before each release:
- Set the version in
pyproject.toml. The Nix package reads this version too. - Update the release notes above.
- Run
just check, thenjust buildandjust check-dist. - Install the wheel in a fresh virtual environment outside the source tree and check that it works.
- Commit the release changes and create an annotated tag, for example
git tag -a v0.1.0 -m "Release 0.1.0". - Run
just publishto upload the checked source archive and wheel to PyPI. - Push the release commit and tag to the repository.
just publish checks the files again but does not rebuild them. It selects
only the two files for the version in pyproject.toml, so older releases in
dist/ are not uploaded. If you change the source, rebuild and check it before
you publish.
For the first upload, create a PyPI account, verify its email address, enable
two-factor authentication, and create an API token with account-wide scope.
Enter the token at Twine's password prompt. After the first upload, replace
that token with a token scoped to zppy. Do not commit tokens to the
repository. See the PyPI publishing guide for account and
authentication details.
Metadata
Release files for zppy 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| zppy-0.1.0.tar.gz | 37.8 kB | Without provenance | Details for zppy-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| zppy-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for zppy-0.1.0-py3-none-any.whl |
Total release size: 54.0 kB
Release files / zppy-0.1.0.tar.gz
| Download URL | zppy-0.1.0.tar.gz |
|---|---|
| Size | 37.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
368e1bf815c8cce14f6348df767ac70cf5b9da5d66b58a8e0a1a9e13c039b530
|
|
BLAKE2b-256 checksum How to use checksums |
f20c86c089fa75cdd9a0d820cd9ead895d9f4cbd9d5b9e5929d1f061c3f3b5bb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.8
|
Release files / zppy-0.1.0-py3-none-any.whl
| Download URL | zppy-0.1.0-py3-none-any.whl |
|---|---|
| Size | 16.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
80507778bb78fe5c0e1d883e4b1188e553deb108706a659e1e0a698622990fca
|
|
BLAKE2b-256 checksum How to use checksums |
6a5e767bb9ac60fc410b85d2bc057a9b83cb9255e15dc6f0d6588b4a610d267c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.8
|
Security for zppy 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.