use std::future::Future;
use std::sync::LazyLock;
use pyo3::prelude::*;
use tokio::runtime::Runtime;
pub static RUNTIME: LazyLock<Runtime> = LazyLock::new(|| {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.expect("failed to create tokio runtime for spvirit-py")
});
pub fn init_async_runtime() {
let _ = pyo3_async_runtimes::tokio::init_with_runtime(&*RUNTIME);
}
pub fn block_on_py<F, T>(py: Python<'_>, fut: F) -> T
where
F: Future<Output = T> + Send,
T: Send,
{
py.allow_threads(|| RUNTIME.block_on(fut))
}
pub fn future_into_py<F, T>(py: Python<'_>, fut: F) -> PyResult<Bound<'_, PyAny>>
where
F: Future<Output = PyResult<T>> + Send + 'static,
T: for<'py> IntoPyObject<'py> + Send + 'static,
{
pyo3_async_runtimes::tokio::future_into_py(py, fut)
}