libpetri for Python
Python bindings for libpetri's Rust runtime. Build Coloured Time Petri Nets with a Python API and execute them on the precompiled Tokio backend through PyO3.
See the project README for the motivation and an order workflow using every arc type, concurrent actions, and timeout routing.
Install
pip install libpetri
Python 3.11 or newer is required. Published wheels contain the Rust extension; no Rust toolchain is needed for normal installation.
Quick start
import libpetri as lp
input_place = lp.Place("input")
output_place = lp.Place("output")
def uppercase(ctx: lp.TransitionContext) -> None:
ctx.output("output", ctx.input("input").upper())
net = (
lp.Net("example")
.transition(
lp.Transition("uppercase")
.input(lp.one(input_place))
.output(lp.out(output_place))
.action(uppercase)
.build()
)
.build()
)
result = lp.run_sync(net, initial={input_place: ["hello"]})
print(result.first(output_place)) # HELLO
Execution and concurrency
Python exposes one production path backed by Rust's owned precompiled net. The executor releases the GIL while running and reacquires it only for Python callbacks.
run_async accepts async def actions. Their awaits are bridged to the loop captured by the caller, but the Tokio worker invoking the callback does not itself have a running asyncio loop. Calls such as asyncio.create_task() or asyncio.get_running_loop() inside an action therefore fail. Prefer structural fan-out into several transitions; use lp.action_gather(...) when several Python awaitables genuinely belong inside one action, and lp.action_to_thread(...) for blocking functions.
Outputs are normally published atomically when an action returns. In an async action, ctx.flush() publishes the current batch early so downstream transitions can run while the action continues. Published batches are not rolled back if the action later fails.
Capabilities
- Input, output, read, inhibitor, and reset arcs.
- Immediate, deadline, delayed, window, and exact timing.
- AND/XOR/timeout routing and input forwarding.
- Reusable subnets, typed interfaces, composition, and place fusion.
- Environment events, event stores, debug protocol, and DOT export.
- ν-net fresh identities and correlated joins.
- Structural, timed, and SMT verification through the Rust engine where available.
Token typing
The package ships .pyi stubs and py.typed, so net construction is IDE- and type-checker-friendly. Token values cross the FFI as Python objects, however: unlike Java, TypeScript, and Rust, Python cannot enforce a place's token type at runtime. Validate data at system boundaries before adding it to a marking.
Build and test from source
python -m pip install -e '.[dev]'
maturin develop
pytest
The extension manifest lives in rust/libpetri-py. Do not build it as an ordinary Cargo workspace binary; maturin supplies the required Python-extension linkage.
Project links
- Language-agnostic specification — 208 active requirements
- Lean soundness and backend-refinement proofs
- Changelog
- Benchmarks
- Apache License 2.0
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file libpetri-3.0.1.tar.gz.
File metadata
- Download URL: libpetri-3.0.1.tar.gz
- Upload date:
- Size: 1.5 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2aee2e32ba18f4fd6a85ad98351b7434f14235c17f57b00f3a11fec5d66e2e56
|
|
| MD5 |
ee3dafe5c8561e4c1b77af83553da5b2
|
|
| BLAKE2b-256 |
9379e94ed08a618c287dea34162a933ab522bf1e91cbd79f2e95c300bfe1c30d
|
File details
Details for the file libpetri-3.0.1-cp312-cp312-manylinux_2_39_x86_64.whl.
File metadata
- Download URL: libpetri-3.0.1-cp312-cp312-manylinux_2_39_x86_64.whl
- Upload date:
- Size: 1.6 MB
- Tags: CPython 3.12, manylinux: glibc 2.39+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f50c37be71ade0badf2b6af88d5e36a0722b3dbe77f14673a07a4d6ec6f332c
|
|
| MD5 |
d087d0918790ad88b643f4d29440d7f7
|
|
| BLAKE2b-256 |
8da6f0e0229a841ad7334b75366f1cf17f698db81e5fcc8bd789c41649432923
|