pyo3-async
PyO3 bindings to various Python asynchronous frameworks.
This crate is deprecated
This crate was an experiment about implementing async support in PyO3.
The experiment was successful, because async support is eventually being implemented at this moment, see the dedicated.
As a consequence, this crate is now deprecated. If you want to try async support before its official release, you should use the branch of the last PR, or master
(where the implementation is still incomplete, but normally stable).
Documentation
How it works
Asynchronous implementations are not so different in Rust and Python. Rust uses callbacks (through std::task::Waker
) to wake up the related executor, while Python Asyncio.Future
also has a callback registered to wake up the event loop.
So, why not use Rust callback to wake up Python event loop, and vice versa ? That's all.
Difference with PyO3 Asyncio
- PyO3 Asyncio requires a running asynchronous runtime on both Python and Rust side, while this crate doesn't;
- PyO3 Asyncio only focus on asyncio, while this crate obviously support asyncio, but also trio or anyio;
- This crate provides control over the GIL release;
- This crate provides
#[pyfunction]
/#[pymethods]
macros.
Example
You can build this module with Maturin
+ Send
// Works with async function
// It generates an `async_sleep_asyncio` function to be exported (see #[pymodule] above)
async
// Specify Python async backend and GIL release
async
// Coroutine can be manually instantiated
async
and execute this Python code
# built with maturin
await
# sleep 1s
await
# sleep 1s
await
# print "done" after 1s
# sleep 1s, print 0
# sleep 1s, print 1
await
# sleep 1s
await
# sleep 1s
# sleep 1s, print 0
# sleep 1s, print 1