You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Rust](http://www.rust-lang.org/) bindings for [Python](https://www.python.org/). This includes running and interacting with Python code from a Rust binary, as well as writing native Python modules.
8
+
[Rust](http://www.rust-lang.org/) bindings for [Python](https://www.python.org/)'s [Asyncio Library](https://docs.python.org/3/library/asyncio.html). This crate facilitates interactions between Rust Futures and Python Coroutines and manages the lifecycle of their corresponding event loops.
9
9
10
10
* API Documentation: [stable](https://docs.rs/pyo3-asyncio/) | [master](https://awestlake87.github.io/pyo3-asyncio/master/doc)
This project contains an example of Python 3 asyncio and Tokio interop. It's
17
-
designed to allow Python complete control over the main thread and facilitate
18
-
interactions between rust and python coroutines.
19
-
20
-
In addition, the `test` module contains a primitive test harness that will run
21
-
a series of rust tests. We cannot use the default test harness because it would
22
-
not allow Python to have control over the main thread. Instead we convert our
23
-
test harness to an `asyncio.Future` and tell the Python `asyncio` event loop to
24
-
drive the future to completion.
25
-
26
-
Allowing Python to have control over the main thread also allows signals to work
27
-
properly. CTRL-C will exit the event loop just as you would expect.
28
-
29
-
Rust futures can be converted into python coroutines and vice versa. When a
30
-
python coroutine raises an exception, the error should be propagated back with
31
-
a `PyResult` as expected. Panics in Rust are translated into python Exceptions
32
-
to propagate through the Python layers. This allows panics in tests to exit the
33
-
event loop like before, but may cause unexpected behaviour if a Rust future
34
-
awaits a Python coroutine that awaits a Rust future that panics.
35
-
36
-
> These scenarios are currently untested in this crate.
14
+
## Quickstart
15
+
16
+
Here we initialize the runtime, import Python's `asyncio` library and run the given future to completion using Python's default `EventLoop` and Tokio. Inside the future, we convert `asyncio` sleep into a Rust future and await it.
17
+
18
+
More details on the usage of this library can be found in the [API docs](https://awestlake87.github.io/pyo3-asyncio/master/doc).
19
+
20
+
```rust no_run
21
+
usepyo3::prelude::*;
22
+
23
+
fnmain() {
24
+
Python::with_gil(|py| {
25
+
// Initialize the runtime
26
+
pyo3_asyncio::with_runtime(py, || {
27
+
letasyncio:PyObject=py.import("asyncio")?.into();
28
+
29
+
// Run the event loop until the given future completes
0 commit comments