py_sync

Macro py_sync 

Source
macro_rules! py_sync {
    ($py: ident, $body: expr) => { ... };
}
Expand description

Spawn and block on a future using the pyo3 tokio runtime. Useful for returning a synchronous PyResult.

When used like the following:

async fn say_hello(name: String) -> String {
    format!("hello {name}")
}

#[pyo3(name="say_hello")]
pub fn py_say_hello(name: String) -> PyResult<String> {
    py_sync!(say_hello(name))
}

Becomes the associated “synchronous” python call:

assert say_hello("Rigetti") == "hello Rigetti"