pytures
Bidirectional bridge between Rust Futures and Python Coroutines, built on PyO3.
Both are poll-based state machines under the hood — pytures adapts one to the other while preserving proper wakeup semantics (no busy-polling when an event loop is available).
Two types
RustCoroutine— wraps a RustFutureas a Python coroutine,await-able from Python.PyAwaitable— wraps a Python awaitable as a RustFuture,.await-able from Rust.
They compose: a RustCoroutine can internally .await a PyAwaitable, and vice versa.
Usage
[]
= "0.1"
= "0.28"
Rust Future as a Python Coroutine
use ;
use RustCoroutine;
// Wrap an async block into a Python-awaitable coroutine.
let coro = attach;
// `coro` can now be passed to Python and awaited there.
Python Coroutine as a Rust Future
use ;
use PyAwaitable;
// Given a Py<PyAny> that is a Python awaitable:
// The returned PyAwaitable implements Future<Output = PyResult<Py<PyAny>>>.
Composing both directions
See examples/select_race.rs for a complete example that races five Python workers (using aiohttp) against each other via tokio::select!, with Rust controlling the sleep timers.
How it works
See INTERNALS.md for details on the coroutine protocol, waker bridging, and state machines.