From f97f42fb4aefc61c48f9fbd33a3f036119a86402 Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Tue, 2 Sep 2025 21:08:31 +0100 Subject: [PATCH] Move some with_gil calls outside of tokio event loop --- src/generic.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/generic.rs b/src/generic.rs index e8853e1..9e50891 100644 --- a/src/generic.rs +++ b/src/generic.rs @@ -596,15 +596,11 @@ where let future_tx1: Py = py_fut.clone().into(); let future_tx2 = future_tx1.clone_ref(py); - R::spawn(async move { - let locals2 = Python::attach(|py| locals.clone_ref(py)); + let (locals2, locals3) = Python::attach(|py| (locals.clone_ref(py), locals.clone_ref(py))); + R::spawn(async move { if let Err(e) = R::spawn(async move { - let result = R::scope( - Python::attach(|py| locals2.clone_ref(py)), - Cancellable::new_with_cancel_rx(fut, cancel_rx), - ) - .await; + let result = R::scope(locals2, Cancellable::new_with_cancel_rx(fut, cancel_rx)).await; Python::attach(move |py| { if cancelled(future_tx1.bind(py)) @@ -615,7 +611,7 @@ where } let _ = set_result( - &locals2.event_loop(py), + &locals3.event_loop(py), future_tx1.bind(py), result.and_then(|val| val.into_py_any(py)), )