Function pyo3_asyncio::tokio::into_coroutine[][src]

pub fn into_coroutine<F>(py: Python<'_>, fut: F) -> PyResult<PyObject> where
    F: Future<Output = PyResult<PyObject>> + Send + 'static, 
👎 Deprecated since 0.14.0:

Use pyo3_asyncio::tokio::future_into_py instead (see the migration guide for more details)

Expand description

Convert a Rust Future into a Python awaitable

Arguments

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

Examples

use std::time::Duration;

use pyo3::prelude::*;

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