Async Runtime Integrations for PyO3
Forked from pyo3-asyncio to deliver compatibility for PyO3 0.21+.
Rust bindings for Python's Asyncio Library. This crate facilitates interactions between Rust Futures and Python Coroutines and manages the lifecycle of their corresponding event loops.
Usage
pyo3-async-runtimes supports the following software versions:
- Python 3.9 and up (CPython and PyPy)
- Rust 1.63 and up
pyo3-async-runtimes Primer
If you are working with a Python library that makes use of async functions or wish to provide
Python bindings for an async Rust library, pyo3-async-runtimes
likely has the tools you need. It provides conversions between async functions in both Python and
Rust and was designed with first-class support for popular Rust runtimes such as
tokio and async-std. In addition, all async Python
code runs on the default asyncio event loop, so pyo3-async-runtimes should work just fine with existing
Python libraries.
In the following sections, we'll give a general overview of pyo3-async-runtimes explaining how to call
async Python functions with PyO3, how to call async Rust functions from Python, and how to configure
your codebase to manage the runtimes of both.
Quickstart
Here are some examples to get you started right away! A more detailed breakdown of the concepts in these examples can be found in the following sections.
Rust Applications
Here we initialize the runtime, import Python's asyncio library and run the given future to completion using Python's default EventLoop and async-std. Inside the future, we convert asyncio sleep into a Rust future and await it.
# Cargo.toml dependencies
[]
= { = "0.27" }
= { = "0.27", = ["attributes", "async-std-runtime"] }
= "1.13"
//! main.rs
use *;
async
The same application can be written to use tokio instead using the #[pyo3_async_runtimes::tokio::main]
attribute.
# Cargo.toml dependencies
[]
= { = "0.27" }
= { = "0.27", = ["attributes", "tokio-runtime"] }
= "1.40"
//! main.rs
use *;
async
More details on the usage of this library can be found in the API docs and the primer below.
PyO3 Native Rust Modules
pyo3-async-runtimes can also be used to write native modules with async functions.
Add the [lib] section to Cargo.toml to make your library a cdylib that Python can import.
[]
= "my_async_module"
= ["cdylib"]
Make your project depend on pyo3 with the extension-module feature enabled and select your
pyo3-async-runtimes runtime:
For async-std:
[]
= { = "0.27", = ["extension-module"] }
= { = "0.27", = ["async-std-runtime"] }
= "1.13"
For tokio:
[]
= { = "0.27", = ["extension-module"] }
= { = "0.27", = ["tokio-runtime"] }
= "1.40"
Export an async function that makes use of async-std:
//! lib.rs
use ;
If you want to use tokio instead, here's what your module should look like:
//! lib.rs
use ;
You can build your module with maturin (see the Using Rust in Python section in the PyO3 guide for setup instructions). After that you should be able to run the Python REPL to try it out.
&&
)
)
>>> import
>>>
>>> from
>>>
>>> async )
>>> await
Awaiting an Async Python Function in Rust
Let's take a look at a dead simple async Python function:
# Sleep for 1 second
await
Async functions in Python are simply functions that return a coroutine object. For our purposes,
we really don't need to know much about these coroutine objects. The key factor here is that calling
an async function is just like calling a regular function, the only difference is that we have
to do something special with the object that it returns.
Normally in Python, that something special is the await keyword, but in order to await this
coroutine in Rust, we first need to convert it into Rust's version of a coroutine: a Future.
That's where pyo3-async-runtimes comes in.
pyo3_async_runtimes::into_future
performs this conversion for us:
use *;
async
If you're interested in learning more about
coroutinesandawaitablesin general, check out the Python 3asynciodocs for more information.
Awaiting a Rust Future in Python
Here we have the same async function as before written in Rust using the
async-std runtime:
/// Sleep for 1 second
async
Similar to Python, Rust's async functions also return a special object called a
Future:
let future = rust_sleep;
We can convert this Future object into Python to make it awaitable. This tells Python that you
can use the await keyword with it. In order to do this, we'll call
pyo3_async_runtimes::async_std::future_into_py:
use *;
async
In Python, we can call this pyo3 function just like any other async function:
await
Managing Event Loops
Python's event loop requires some special treatment, especially regarding the main thread. Some of
Python's asyncio features, like proper signal handling, require control over the main thread, which
doesn't always play well with Rust.
Luckily, Rust's event loops are pretty flexible and don't need control over the main thread, so in
pyo3-async-runtimes, we decided the best way to handle Rust/Python interop was to just surrender the main
thread to Python and run Rust's event loops in the background. Unfortunately, since most event loop
implementations prefer control over the main thread, this can still make some things awkward.
pyo3-async-runtimes Initialization
Because Python needs to control the main thread, we can't use the convenient proc macros from Rust
runtimes to handle the main function or #[test] functions. Instead, the initialization for PyO3 has to be done from the main function and the main
thread must block on pyo3_async_runtimes::async_std::run_until_complete.
Because we have to block on one of those functions, we can't use #[async_std::main] or #[tokio::main]
since it's not a good idea to make long blocking calls during an async function.
Internally, these
#[main]proc macros are expanded to something like this:Making a long blocking call inside the
Futurethat's being driven byblock_onprevents that thread from doing anything else and can spell trouble for some runtimes (also this will actually deadlock a single-threaded runtime!). Many runtimes have some sort ofspawn_blockingmechanism that can avoid this problem, but again that's not something we can use here since we need it to block on the main thread.
For this reason, pyo3-async-runtimes provides its own set of proc macros to provide you with this
initialization. These macros are intended to mirror the initialization of async-std and tokio
while also satisfying the Python runtime's needs.
Here's a full example of PyO3 initialization with the async-std runtime:
use *;
async
A Note About asyncio.run
In Python 3.7+, the recommended way to run a top-level coroutine with asyncio
is with asyncio.run. In v0.13 we recommended against using this function due to initialization issues, but in v0.14 it's perfectly valid to use this function... with a caveat.
Since our Rust <--> Python conversions require a reference to the Python event loop, this poses a problem. Imagine we have a pyo3-async-runtimes module that defines
a rust_sleep function like in previous examples. You might rightfully assume that you can call pass this directly into asyncio.run like this:
You might be surprised to find out that this throws an error:
)
))
What's happening here is that we are calling rust_sleep before the future is
actually running on the event loop created by asyncio.run. This is counter-intuitive, but expected behaviour, and unfortunately there doesn't seem to be a good way of solving this problem within pyo3-async-runtimes itself.
However, we can make this example work with a simple workaround:
# Calling main will just construct the coroutine that later calls rust_sleep.
# - This ensures that rust_sleep will be called when the event loop is running,
# not before.
await
# Run the main() coroutine at the top-level instead
Non-standard Python Event Loops
Python allows you to use alternatives to the default asyncio event loop. One
popular alternative is uvloop. In v0.13 using non-standard event loops was
a bit of an ordeal, but in v0.14 it's trivial.
Using uvloop in a PyO3 Native Extensions
# Cargo.toml
[]
= "my_async_module"
= ["cdylib"]
[]
= { = "0.27", = ["extension-module"] }
= { = "0.27", = ["tokio-runtime"] }
= "1.13"
= "1.40"
//! lib.rs
use ;
&&
)
)
>>> import
>>> import
>>>
>>> import
>>>
>>> uvloop.install()
>>>
>>> async )
)
>>> asyncio.run(
Using uvloop in Rust Applications
Using uvloop in Rust applications is a bit trickier, but it's still possible
with relatively few modifications.
Unfortunately, we can't make use of the #[pyo3_async_runtimes::<runtime>::main] attribute with non-standard event loops. This is because the #[pyo3_async_runtimes::<runtime>::main] proc macro has to interact with the Python
event loop before we can install the uvloop policy.
[]
= "1.13"
= "0.27"
= { = "0.27", = ["async-std-runtime"] }
//! main.rs
use ;
Additional Information
- Managing event loop references can be tricky with
pyo3-async-runtimes. See Event Loop References and ContextVars in the API docs to get a better intuition for how event loop references are managed in this library. - Testing
pyo3-async-runtimeslibraries and applications requires a custom test harness since Python requires control over the main thread. You can find a testing guide in the API docs for thetestingmodule