8000 use std::sync::OnceLock to store tokio runtime instead of round-tripp… · apache/datafusion-python@6fa05ba · GitHub
[go: up one dir, main page]

Skip to content

Commit 6fa05ba

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

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

src/utils.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,16 @@ 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) => {
32-
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-
}
27+
pub(crate) fn get_tokio_runtime(_: Python) -> Arc<TokioRuntime> {
28+
static RUNTIME: OnceLock<Arc<TokioRuntime>> = OnceLock::new();
29+
RUNTIME.get_or_init(|| {
30+
let rt = TokioRuntime(tokio::runtime::Runtime::new().unwrap());
31+
Arc::new(rt)
32+
}).clone()
3733
}
3834

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

0 commit comments

Comments
 (0)
0