Function pyo3_asyncio::generic::local_cancellable_future_into_py_with_loop[][src]

pub fn local_cancellable_future_into_py_with_loop<R, F>(
    event_loop: &PyAny,
    fut: F
) -> PyResult<&PyAny> where
    R: Runtime + SpawnLocalExt + LocalContextExt,
    F: Future<Output = PyResult<PyObject>> + 'static, 
👎 Deprecated since 0.15.0:

Use pyo3_asyncio::generic::local_future_into_py_with_locals instead

Expand description

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

This function was deprecated in favor of local_future_into_py_with_locals in v0.15 because it became the default behaviour. In v0.15, any calls to this function should be replaced with local_future_into_py_with_locals.

This function will be removed in v0.16

Arguments

  • event_loop - The Python event loop that the awaitable should be attached to
  • fut - The Rust future to be converted

Examples

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

use pyo3::prelude::*;

/// Awaitable sleep function
#[pyfunction]
fn sleep_for<'p>(py: Python<'p>, secs: u64) -> PyResult<&'p 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_cancellable_future_into_py_with_loop::<MyCustomRuntime, _>(
        pyo3_asyncio::get_running_loop(py)?,
        async move {
            MyCustomRuntime::sleep(Duration::from_secs(*secs)).await;
            Python::with_gil(|py| Ok(py.None()))
        }
    )
}