Michael-J-Ward commented on code in PR #895:
URL: https://github.com/apache/datafusion-python/pull/895#discussion_r1792406544


##########
src/utils.rs:
##########
@@ -20,20 +20,19 @@ use crate::TokioRuntime;
 use datafusion::logical_expr::Volatility;
 use pyo3::prelude::*;
 use std::future::Future;
+use std::sync::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) => {
-            let rt = TokioRuntime(tokio::runtime::Runtime::new().unwrap());
-            let obj: Bound<'_, TokioRuntime> = Py::new(py, 
rt).unwrap().into_bound(py);
-            obj.extract().unwrap()
-        }
-    }
+#[inline]
+pub(crate) fn get_tokio_runtime() -> &'static TokioRuntime {
+    // NOTE: Other pyo3 python libraries have had issues with using tokio
+    // behind a forking app-server like `gunicorn`
+    // If we run into that problem, in the future we can look to `delta-rs`
+    // which adds a check in that disallows calls from a forked process
+    // 
https://github.com/delta-io/delta-rs/blob/87010461cfe01563d91a4b9cd6fa468e2ad5f283/python/src/utils.rs#L10-L31
+    static RUNTIME: OnceLock<TokioRuntime> = OnceLock::new();

Review Comment:
   I recommend looking at the PR diff to see the real change.
   
   There is an intermediate commit that went from  `OnceLock<Arc<Runtime>>` to 
`OnceLock<Runtime>`, but that's because the `Arc` was superfluous.
   
   `OnceLock` is already `Send + Sync` and explicitly 
[thread-safe](https://doc.rust-lang.org/stable/std/sync/struct.OnceLock.html). 
   
   So, if you discover that it is **not** actually thread-safe, please share!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to