Function pyo3_asyncio::async_std::local_cancellable_future_into_py[][src]

pub fn local_cancellable_future_into_py<F>(
    py: Python<'_>,
    fut: F
) -> PyResult<&PyAny> where
    F: Future<Output = PyResult<PyObject>> + 'static, 
👎 Deprecated since 0.15.0:

Use pyo3_asyncio::async_std::local_future_into_py instead

Expand description

Convert a !Send Rust Future into a Python awaitable

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

This function will be removed in v0.16

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 non-send sleep function
#[pyfunction]
fn sleep_for(py: Python, secs: u64) -> PyResult<&PyAny> {
    // Rc is non-send so it cannot be passed into pyo3_asyncio::async_std::future_into_py
    let secs = Rc::new(secs);
    pyo3_asyncio::async_std::local_cancellable_future_into_py(py, async move {
        async_std::task::sleep(Duration::from_secs(*secs)).await;
        Python::with_gil(|py| Ok(py.None()))
    })
}

#[pyo3_asyncio::async_std::main]
async fn main() -> PyResult<()> {
    Python::with_gil(|py| {
        let py_future = sleep_for(py, 1)?;
        pyo3_asyncio::async_std::into_future(py_future)
    })?
    .await?;

    Ok(())
}