Skip to content

Commit

Permalink
use std::sync::OnceLock to store tokio runtime instead of round-tripp…
Browse files Browse the repository at this point in the history
…ing to python
  • Loading branch information
Michael-J-Ward committed Oct 3, 2024
1 parent a2c90b8 commit ff1e758
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,18 @@ use crate::TokioRuntime;
use datafusion::logical_expr::Volatility;
use pyo3::prelude::*;
use std::future::Future;
use std::sync::{Arc, OnceLock};
use tokio::runtime::Runtime;

/// Utility to get the Tokio Runtime from Python
pub(crate) fn get_tokio_runtime(py: Python) -> PyRef<TokioRuntime> {
let datafusion = py.import_bound("datafusion._internal").unwrap();
let tmp = datafusion.getattr("runtime").unwrap();
match tmp.extract::<PyRef<TokioRuntime>>() {
Ok(runtime) => runtime,
Err(_e) => {
pub(crate) fn get_tokio_runtime(_: Python) -> Arc<TokioRuntime> {
static RUNTIME: OnceLock<Arc<TokioRuntime>> = OnceLock::new();
RUNTIME
.get_or_init(|| {
let rt = TokioRuntime(tokio::runtime::Runtime::new().unwrap());
let obj: Bound<'_, TokioRuntime> = Py::new(py, rt).unwrap().into_bound(py);
obj.extract().unwrap()
}
}
Arc::new(rt)
})
.clone()
}

/// Utility to collect rust futures with GIL released
Expand Down

0 comments on commit ff1e758

Please sign in to comment.