Skip to content

Commit ff1e758

Browse files
use std::sync::OnceLock to store tokio runtime instead of round-tripping to python
1 parent a2c90b8 commit ff1e758

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

src/utils.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,18 @@ use crate::TokioRuntime;
2020
use datafusion::logical_expr::Volatility;
2121
use pyo3::prelude::*;
2222
use std::future::Future;
23+
use std::sync::{Arc, OnceLock};
2324
use tokio::runtime::Runtime;
2425

2526
/// Utility to get the Tokio Runtime from Python
26-
pub(crate) fn get_tokio_runtime(py: Python) -> PyRef<TokioRuntime> {
27-
let datafusion = py.import_bound("datafusion._internal").unwrap();
28-
let tmp = datafusion.getattr("runtime").unwrap();
29-
match tmp.extract::<PyRef<TokioRuntime>>() {
30-
Ok(runtime) => runtime,
31-
Err(_e) => {
27+
pub(crate) fn get_tokio_runtime(_: Python) -> Arc<TokioRuntime> {
28+
static RUNTIME: OnceLock<Arc<TokioRuntime>> = OnceLock::new();
29+
RUNTIME
30+
.get_or_init(|| {
3231
let rt = TokioRuntime(tokio::runtime::Runtime::new().unwrap());
33-
let obj: Bound<'_, TokioRuntime> = Py::new(py, rt).unwrap().into_bound(py);
34-
obj.extract().unwrap()
35-
}
36-
}
32+
Arc::new(rt)
33+
})
34+
.clone()
3735
}
3836

3937
/// Utility to collect rust futures with GIL released

0 commit comments

Comments
 (0)