Function pyo3_asyncio::run_forever[][src]

pub fn run_forever(py: Python<'_>) -> PyResult<()>
👎 Deprecated since 0.14.0:

see the migration guide for more details

Expand description

Run the event loop forever

This can be called instead of run_until_complete to run the event loop until stop is called rather than driving a future to completion.

After this function returns, the event loop can be resumed with either run_until_complete or crate::run_forever

Arguments

  • py - The current PyO3 GIL guard

Examples

fn main() -> pyo3::PyResult<()> {
    use std::time::Duration;
    use pyo3::prelude::*;
     
    // call this or use pyo3 0.14 "auto-initialize" feature
    pyo3::prepare_freethreaded_python();

    Python::with_gil(|py| {
        pyo3_asyncio::with_runtime(py, || {
            let event_loop_hdl = PyObject::from(pyo3_asyncio::get_event_loop(py));
            // Wait 1 second, then stop the event loop
            async_std::task::spawn(async move {
                async_std::task::sleep(Duration::from_secs(1)).await;
                Python::with_gil(|py| {
                    event_loop_hdl
                        .as_ref(py)
                        .call_method1(
                            "call_soon_threadsafe",
                            (event_loop_hdl
                                .as_ref(py)
                                .getattr("stop")
                                .map_err(|e| e.print_and_set_sys_last_vars(py))
                                .unwrap(),),
                            )
                            .unwrap();
                })
            });
     
            pyo3_asyncio::run_forever(py)?;

            Ok(())
        })
    })
}