### PyO3 Initialization with async-std Runtime Source: https://github.com/pyo3/pyo3-async-runtimes/blob/main/README.md Provides a complete example of initializing PyO3 with the async-std runtime using the `#[pyo3_async_runtimes::async_std::main]` macro. This setup is necessary for proper event loop management when integrating Rust async code with Python. ```rust use pyo3::prelude::*; #[pyo3_async_runtimes::async_std::main] async fn main() -> PyResult<()> { // PyO3 is initialized - Ready to go let fut = Python::with_gil(|py| -> PyResult<_> { let asyncio = py.import("asyncio")?; // convert asyncio.sleep into a Rust Future pyo3_async_runtimes::async_std::into_future( asyncio.call_method1("sleep", (1.into_pyobject(py).unwrap(),))? ) })?; fut.await?; Ok(()) } ``` -------------------------------- ### Bash/Python: Development and Testing Workflow Source: https://github.com/pyo3/pyo3-async-runtimes/blob/main/README.md Illustrates the command-line steps to build a Python extension using maturin and then interact with it in a Python interpreter, including installing uvloop and running async code. ```Bash $ maturin develop && python3 🔗 Found pyo3 bindings 🐍 Found CPython 3.8 at python3 Finished dev [unoptimized + debuginfo] target(s) in 0.04s Python 3.8.8 (default, Apr 13 2021, 19:58:26) [GCC 7.3.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. >>> import asyncio >>> import uvloop >>> >>> import my_async_module >>> >>> uvloop.install() >>> >>> async def main(): ... await my_async_module.rust_sleep() ... >>> asyncio.run(main()) >>> ``` -------------------------------- ### Rust tokio Runtime Integration with Python Asyncio Source: https://github.com/pyo3/pyo3-async-runtimes/blob/main/README.md Illustrates the integration of `pyo3-async-runtimes` with the `tokio` runtime. This example uses the `#[pyo3_async_runtimes::tokio::main]` attribute to initialize the `tokio` runtime, imports Python's `asyncio` library, and converts a Python `asyncio.sleep` call into a Rust Future using `pyo3_async_runtimes::tokio::into_future` for asynchronous execution. ```toml # Cargo.toml dependencies [dependencies] pyo3 = { version = "0.25" } pyo3-async-runtimes = { version = "0.25", features = ["attributes", "tokio-runtime"] } tokio = "1.40" ``` ```rust !/main.rs use pyo3::prelude::*; #[pyo3_async_runtimes::tokio::main] async fn main() -> PyResult<()> { let fut = Python::with_gil(|py| { let asyncio = py.import("asyncio")?; // convert asyncio.sleep into a Rust Future pyo3_async_runtimes::tokio::into_future(asyncio.call_method1("sleep", (1.into_pyobject(py).unwrap(),))?) })?; fut.await?; Ok(()) } ``` -------------------------------- ### Rust: Using uvloop in Rust Applications Source: https://github.com/pyo3/pyo3-async-runtimes/blob/main/README.md Demonstrates how to use Python's uvloop within a standalone Rust application that also uses pyo3-async-runtimes. It involves manually installing the uvloop policy before running async tasks, bypassing the proc-macro convenience for non-standard loops. ```Rust // main.rs use pyo3::{prelude::*, types::PyType}; fn main() -> PyResult<()> { pyo3::prepare_freethreaded_python(); Python::with_gil(|py| { let uvloop = py.import("uvloop")?; uvloop.call_method0("install")?; // store a reference for the assertion let uvloop = PyObject::from(uvloop); pyo3_async_runtimes::async_std::run(py, async move { // verify that we are on a uvloop.Loop Python::with_gil(|py| -> PyResult<()> { assert!(uvloop .bind(py) .getattr("Loop")? .downcast::() .unwrap() .is_instance(&pyo3_async_runtimes::async_std::get_current_loop(py)?)?); Ok(()) })?; async_std::task::sleep(std::time::Duration::from_secs(1)).await; Ok(()) }) }) } ``` -------------------------------- ### Python Async Function Example Source: https://github.com/pyo3/pyo3-async-runtimes/blob/main/README.md A simple Python asynchronous function that simulates work by sleeping for one second using `asyncio.sleep`. This serves as an example of a Python coroutine that can be awaited from Rust. ```python # Sleep for 1 second async def py_sleep(): await asyncio.sleep(1) ``` -------------------------------- ### Build and Test Native Module Source: https://github.com/pyo3/pyo3-async-runtimes/blob/main/README.md Shows the command to build the Rust extension module using maturin and then how to interact with it in a Python REPL, demonstrating the execution of an async Rust function. ```bash maturin develop && python3 # ... build output ... Python 3.8.5 (default, Jan 27 2021, 15:41:15) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import asyncio >>> >>> from my_async_module import rust_sleep >>> >>> async def main(): >>> await rust_sleep() >>> >>> # should sleep for 1s >>> asyncio.run(main()) >>> ``` -------------------------------- ### Configure Cargo.toml for cdylib Source: https://github.com/pyo3/pyo3-async-runtimes/blob/main/README.md Sets up the Cargo.toml file to build a Rust library as a cdylib, which is necessary for Python to import it as a native module. ```toml [lib] name = "my_async_module" crate-type = ["cdylib"] ``` -------------------------------- ### Rust async-std Runtime Integration with Python Asyncio Source: https://github.com/pyo3/pyo3-async-runtimes/blob/main/README.md Demonstrates how to use the `pyo3-async-runtimes` crate with the `async-std` runtime. It shows setting up the `async-std` runtime using the `#[pyo3_async_runtimes::async_std::main]` attribute, importing Python's `asyncio` module, converting a Python `asyncio.sleep` call into a Rust Future using `pyo3_async_runtimes::async_std::into_future`, and awaiting its completion. ```toml # Cargo.toml dependencies [dependencies] pyo3 = { version = "0.25" } pyo3-async-runtimes = { version = "0.25", features = ["attributes", "async-std-runtime"] } async-std = "1.13" ``` ```rust !/main.rs use pyo3::prelude::*; #[pyo3_async_runtimes::async_std::main] async fn main() -> PyResult<()> { let fut = Python::with_gil(|py| { let asyncio = py.import("asyncio")?; // convert asyncio.sleep into a Rust Future pyo3_async_runtimes::async_std::into_future(asyncio.call_method1("sleep", (1.into_pyobject(py).unwrap(),))?) })?; fut.await?; Ok(()) } ``` -------------------------------- ### Git Hooks Configuration Source: https://github.com/pyo3/pyo3-async-runtimes/blob/main/Contributing.md Enables project-specific Git hooks to automate checks like code formatting and testing before commits or pushes, preventing common CI failures. ```git git config core.hookspath .githooks ``` -------------------------------- ### TOML: Dependencies for uvloop with async-std Source: https://github.com/pyo3/pyo3-async-runtimes/blob/main/README.md Lists the required dependencies in Cargo.toml for a Rust project that uses pyo3-async-runtimes with the async-std runtime and aims to integrate with Python's uvloop. ```TOML [dependencies] async-std = "1.13" pyo3 = "0.25" pyo3-async-runtimes = { version = "0.25", features = ["async-std-runtime"] } ``` -------------------------------- ### Add Dependencies for Tokio Runtime Source: https://github.com/pyo3/pyo3-async-runtimes/blob/main/README.md Specifies the required dependencies in Cargo.toml for integrating pyo3-async-runtimes with the tokio runtime, including pyo3 and tokio itself. ```toml [dependencies] pyo3 = { version = "0.25", features = ["extension-module"] } pyo3-async-runtimes = { version = "0.25", features = ["tokio-runtime"] } tokio = "1.40" ``` -------------------------------- ### TOML: Dependencies for uvloop with Tokio Source: https://github.com/pyo3/pyo3-async-runtimes/blob/main/README.md Specifies the necessary dependencies in Cargo.toml for a Rust project that uses pyo3-async-runtimes with the Tokio runtime and integrates with Python's uvloop. ```TOML # Cargo.toml [lib] name = "my_async_module" crate-type = ["cdylib"] [dependencies] pyo3 = { version = "0.25", features = ["extension-module"] } pyo3-async-runtimes = { version = "0.25", features = ["tokio-runtime"] } async-std = "1.13" tokio = "1.40" ``` -------------------------------- ### Add Dependencies for async-std Runtime Source: https://github.com/pyo3/pyo3-async-runtimes/blob/main/README.md Specifies the required dependencies in Cargo.toml for integrating pyo3-async-runtimes with the async-std runtime, including pyo3 and async-std itself. ```toml [dependencies] pyo3 = { version = "0.25", features = ["extension-module"] } pyo3-async-runtimes = { version = "0.25", features = ["async-std-runtime"] } async-std = "1.13" ``` -------------------------------- ### Testing pyo3-async-runtimes Applications Source: https://github.com/pyo3/pyo3-async-runtimes/blob/main/README.md Guidance on creating a custom test harness for libraries and applications built with pyo3-async-runtimes. This is necessary because Python requires specific control over the main thread during testing. ```APIDOC Testing Module: - Provides a custom test harness for pyo3-async-runtimes. - Addresses the requirement for Python to control the main thread during testing. - Offers strategies and examples for setting up a robust testing environment. - Link: https://docs.rs/pyo3-async-runtimes/latest/pyo3_async_runtimes/testing/index.html ``` -------------------------------- ### Await Rust Function in Python Source: https://github.com/pyo3/pyo3-async-runtimes/blob/main/README.md Demonstrates how to call and `await` a Rust function that returns an awaitable object from Python. This enables Python's asynchronous code to seamlessly interact with Rust's async tasks. ```python from example import call_rust_sleep async def rust_sleep(): await call_rust_sleep() ``` -------------------------------- ### Event Loop References and ContextVars Source: https://github.com/pyo3/pyo3-async-runtimes/blob/main/README.md Details on managing event loop references within the context of pyo3-async-runtimes. This section is crucial for understanding how asynchronous operations interact with Python's event loop and context variables. ```APIDOC Event Loop References and ContextVars: - Manages event loop references across Python and Rust boundaries. - Explains the role of ContextVars in maintaining asynchronous state. - Provides insights into potential pitfalls and best practices for reference management. - Link: https://docs.rs/pyo3-async-runtimes/latest/pyo3_async_runtimes/#event-loop-references-and-contextvars ``` -------------------------------- ### Export Async Rust Function with Tokio Source: https://github.com/pyo3/pyo3-async-runtimes/blob/main/README.md Demonstrates how to export an asynchronous Rust function from a pyo3 extension module using the tokio runtime. It uses `future_into_py` to bridge Rust futures with Python awaitables. ```rust //! lib.rs use pyo3::{prelude::*, wrap_pyfunction}; #[pyfunction] fn rust_sleep(py: Python) -> PyResult> { pyo3_async_runtimes::tokio::future_into_py(py, async { tokio::time::sleep(std::time::Duration::from_secs(1)).await; Ok(()) }) } #[pymodule] fn my_async_module(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_function(wrap_pyfunction!(rust_sleep, m)?) Ok(()) } ``` -------------------------------- ### Export Async Rust Function with async-std Source: https://github.com/pyo3/pyo3-async-runtimes/blob/main/README.md Demonstrates how to export an asynchronous Rust function from a pyo3 extension module using the async-std runtime. It uses `future_into_py` to bridge Rust futures with Python awaitables. ```rust //! lib.rs use pyo3::{prelude::*, wrap_pyfunction}; #[pyfunction] fn rust_sleep(py: Python) -> PyResult> { pyo3_async_runtimes::async_std::future_into_py(py, async { async_std::task::sleep(std::time::Duration::from_secs(1)).await; Ok(()) }) } #[pymodule] fn my_async_module(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_function(wrap_pyfunction!(rust_sleep, m)?) Ok(()) } ``` -------------------------------- ### Python: Running asyncio.run with pyo3-async-runtimes Source: https://github.com/pyo3/pyo3-async-runtimes/blob/main/README.md Demonstrates a common issue when running Rust async functions with Python's `asyncio.run` and provides a workaround. It highlights the need to structure coroutines correctly to ensure the event loop is active when the async Rust function is called. ```Python import asyncio from my_async_module import rust_sleep async def main(): await rust_sleep() asyncio.run(main()) ``` -------------------------------- ### Rust: Tokio-based async function for pyo3 Source: https://github.com/pyo3/pyo3-async-runtimes/blob/main/README.md Provides a Rust implementation of an asynchronous function designed to be called from Python using pyo3-async-runtimes with the Tokio runtime. It uses `tokio::time::sleep` and converts the Rust future into a Python awaitable object. ```Rust // lib.rs use pyo3::{prelude::*, wrap_pyfunction}; #[pyfunction] fn rust_sleep(py: Python) -> PyResult> { pyo3_async_runtimes::tokio::future_into_py(py, async { tokio::time::sleep(std::time::Duration::from_secs(1)).await; Ok(()) }) } #[pymodule] fn my_async_module(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_function(wrap_pyfunction!(rust_sleep, m)?) Ok(()) } ``` -------------------------------- ### Convert Rust Future to Python Awaitable Source: https://github.com/pyo3/pyo3-async-runtimes/blob/main/README.md Shows how to convert a Rust `Future` into a Python awaitable object using `pyo3_async_runtimes::async_std::future_into_py`. This allows Python's `await` keyword to be used with Rust async operations. ```rust use pyo3::prelude::*; async fn rust_sleep() { async_std::task::sleep(std::time::Duration::from_secs(1)).await; } #[pyfunction] fn call_rust_sleep(py: Python) -> PyResult> { pyo3_async_runtimes::async_std::future_into_py(py, async move { rust_sleep().await; Ok(()) }) } ``` -------------------------------- ### Await Python Coroutine in Rust with Tokio Source: https://github.com/pyo3/pyo3-async-runtimes/blob/main/README.md Illustrates how to call a Python asynchronous function from Rust and await its result. It uses `pyo3_async_runtimes::tokio::into_future` to convert the Python coroutine object into a Rust `Future` compatible with the tokio runtime. ```rust use pyo3::prelude::*; #[pyo3_async_runtimes::tokio::main] async fn main() -> PyResult<()> { let future = Python::with_gil(|py| -> PyResult<_> { // import the module containing the py_sleep function let example = py.import("example")?; // calling the py_sleep method like a normal function // returns a coroutine let coroutine = example.call_method0("py_sleep")?; // convert the coroutine into a Rust future using the // tokio runtime pyo3_async_runtimes::tokio::into_future(coroutine) })?; // await the future future.await?; Ok(()) } ``` -------------------------------- ### Define Rust Async Function Source: https://github.com/pyo3/pyo3-async-runtimes/blob/main/README.md Demonstrates a simple asynchronous function in Rust using the async-std runtime. It uses `async_std::task::sleep` to pause execution for a specified duration. ```rust /// Sleep for 1 second async fn rust_sleep() { async_std::task::sleep(std::time::Duration::from_secs(1)).await; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.