Something went wrong. Try again.
An experimental alternative to Rust sans-io that enables the direct style
Something went wrong. Try again.
1.7 kB · 47 lines
Python
at experiments
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748"""Runs alice and bob as capability greeters through the PyO3 extension moduleon one loop thread — the generated-bindings reading, beside ``../ctypes``.
Diff the two directories: ``codec.py`` and ``native.py`` are gone, and of``../abi`` only the scheduler is used. PyO3 made ``Greeter``, ``Effect`` and``Status`` Python types and ``Finished``/``BadInput`` Python exceptions, sothere is nothing to decode and no code to interpret. What moved: a Rust cratethat knows about Python (``../../rust/pyo3``), a build that needs a Pythoninterpreter, a ``.so`` only Python can load. The ``ctypes`` host paid for thesame program in host code instead.
The ``Greeter`` is ``unsendable`` — PyO3's word for the thread-affine driverunderneath — so the binding layer itself enforces what ``ABI.md``'s threadingrow states: touch it from another thread and the call aborts.
Usage: ``PYTHONPATH=<dir containing capabilities_pyo3.so>:.. python3 main.py``"""
from __future__ import annotations
import sys
from capabilities_pyo3 import Greeterfrom driver import Driverfrom host import Hostfrom abi.scheduler import Scheduler, run_all
ALICE = ["World", "Monde", "Welt"]BOB = ["Mundo", "Hans"]CAROL = ["Inés", "Amélie"]
def main() -> int: machines = [ Driver(Host("alice", ALICE)), Driver(Host("bob", BOB)), # Two requests per batch; the same driver, because it queues answers. Driver(Host("carol", CAROL), make=Greeter.fanout), ] elapsed = run_all((Scheduler.sharded(1), machines))
print(f"-- 2 capability greeters + 1 fan-out program via PyO3 on 1 loop thread + timers: {elapsed:.1f} ms wall") return 0
if __name__ == "__main__": sys.exit(main())