Function pyo3_asyncio::generic::local_future_into_py[][src]

pub fn local_future_into_py<R, F>(py: Python<'_>, fut: F) -> PyResult<&PyAny> where
    R: SpawnLocalExt,
    F: Future<Output = PyResult<PyObject>> + 'static, 
Expand description

Convert a !Send Rust Future into a Python awaitable with a generic runtime

Arguments

  • py - The current PyO3 GIL guard
  • fut - The Rust future to be converted

Examples

use std::{rc::Rc, time::Duration};

use pyo3::prelude::*;

/// Awaitable sleep function
#[pyfunction]
fn sleep_for(py: Python, secs: u64) -> PyResult<&PyAny> {
    // Rc is !Send so it cannot be passed into pyo3_asyncio::generic::future_into_py
    let secs = Rc::new(secs);

    pyo3_asyncio::generic::local_future_into_py::<MyCustomRuntime, _>(py, async move {
        MyCustomRuntime::sleep(Duration::from_secs(*secs)).await;
        Python::with_gil(|py| Ok(py.None()))
    })
}