Function into_future

Source
pub fn into_future<R>(
    awaitable: Bound<'_, PyAny>,
) -> PyResult<impl Future<Output = PyResult<PyObject>> + Send>
where R: Runtime + ContextExt,
Expand description

Convert a Python awaitable into a Rust Future

This function simply forwards the future and the task locals returned by get_current_locals to into_future_with_locals. See into_future_with_locals for more details.

§Arguments

  • awaitable - The Python awaitable to be converted

§Examples

const PYTHON_CODE: &'static str = r#"
import asyncio

async def py_sleep(duration):
    await asyncio.sleep(duration)
"#;

async fn py_sleep(seconds: f32) -> PyResult<()> {
    let test_mod = Python::with_gil(|py| -> PyResult<PyObject> {
        Ok(
            PyModule::from_code(
                py,
                &CString::new(PYTHON_CODE).unwrap(),
                &CString::new("test_into_future/test_mod.py").unwrap(),
                &CString::new("test_mod").unwrap(),
            )?
            .into()
        )
    })?;

    Python::with_gil(|py| {
        pyo3_async_runtimes::generic::into_future::<MyCustomRuntime>(
            test_mod
                .call_method1(py, "py_sleep", (seconds.into_pyobject(py).unwrap(),))?
                .into_bound(py),
        )
    })?
    .await?;
    Ok(())
}