Attribute Macro pyfunction

Source
#[pyfunction]
Expand description

pyo3::pyfunction with async support.

Generate a additional function prefixed by async_, decorated by pyo3::pyfunction and #[pyo3(name = ...)].

Python async backend can be specified using macro argument (default to asyncio). If allow_threads is passed in arguments, GIL will be released for future polling (see AllowThreads)

ยงExample

#[pyo3_async::pyfunction(allow_threads)]
pub async fn print(s: String) {
    println!("{s}");
}

generates

pub async fn print(s: String) {
    println!("{s}");
}
#[::pyo3::pyfunction]
#[pyo3(name = "print")]
pub fn async_print(s: String) -> ::pyo3_async::asyncio::Coroutine {
    ::pyo3_async::asyncio::Coroutine::from_future(::pyo3_async::AllowThreads(
        async move { print(s).await; Ok(()) }
    ))
}