use std::sync::LazyLock;
use pyo3::prelude::*;
use tokio::runtime as rt;
use tokio::task::JoinHandle;
pub(crate) static GIL_RUNTIME: LazyLock<rt::Runtime> = LazyLock::new(|| {
let mut builder = rt::Builder::new_multi_thread();
#[cfg(not(Py_GIL_DISABLED))]
{
builder.worker_threads(1);
}
let thread_name = format!("{}-gil-rt", env!("CARGO_PKG_NAME"));
let runtime = builder
.enable_all()
.thread_name(&thread_name)
.build()
.unwrap_or_else(|e| panic!("Failed to create the `{thread_name}` tokio runtime: {e}"));
runtime
});
pub(crate) fn task_with_gil<F, R>(f: F) -> JoinHandle<R>
where
F: for<'py> FnOnce(Python<'py>) -> R + Send + 'static,
R: Send + 'static,
{
let future = async move { Python::with_gil(f) };
GIL_RUNTIME.spawn(future)
}