Function pyo3_asyncio::async_std::future_into_py_with_loop[][src]

pub fn future_into_py_with_loop<F>(
    event_loop: &PyAny,
    fut: F
) -> PyResult<&PyAny> where
    F: Future<Output = PyResult<PyObject>> + Send + 'static, 
Expand description

Convert a Rust Future into a Python awaitable

Arguments

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

Examples

use std::time::Duration;

use pyo3::prelude::*;

/// Awaitable sleep function
#[pyfunction]
fn sleep_for<'p>(py: Python<'p>, secs: &'p PyAny) -> PyResult<&'p PyAny> {
    let secs = secs.extract()?;
    pyo3_asyncio::async_std::future_into_py_with_loop(
        pyo3_asyncio::async_std::get_current_loop(py)?,
        async move {
            async_std::task::sleep(Duration::from_secs(secs)).await;
            Python::with_gil(|py| Ok(py.None()))
        }
    )
}