Skip to content

Commit 78819d3

Browse files
committed
Wrap task locals loop and context in Arc.
1 parent 5ce66d1 commit 78819d3

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

src/async_std.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl ContextExt for AsyncStdRuntime {
9494
.try_with(|c| {
9595
c.borrow()
9696
.as_ref()
97-
.map(|locals| Python::attach(|py| locals.clone_ref(py)))
97+
.map(|locals| locals.clone_ref())
9898
})
9999
.unwrap_or_default()
100100
}

src/generic.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ where
9191
R: ContextExt,
9292
{
9393
if let Some(locals) = R::get_task_locals() {
94-
Ok(locals.event_loop.into_bound(py))
94+
Ok(locals.event_loop.clone_ref(py).into_bound(py))
9595
} else {
9696
get_running_loop(py)
9797
}
@@ -597,11 +597,11 @@ where
597597
let future_tx2 = future_tx1.clone_ref(py);
598598

599599
R::spawn(async move {
600-
let locals2 = Python::attach(|py| locals.clone_ref(py));
600+
let locals2 = locals.clone_ref();
601601

602602
if let Err(e) = R::spawn(async move {
603603
let result = R::scope(
604-
Python::attach(|py| locals2.clone_ref(py)),
604+
locals2.clone_ref(),
605605
Cancellable::new_with_cancel_rx(fut, cancel_rx),
606606
)
607607
.await;
@@ -1002,11 +1002,11 @@ where
10021002
let future_tx2 = future_tx1.clone_ref(py);
10031003

10041004
R::spawn_local(async move {
1005-
let locals2 = Python::attach(|py| locals.clone_ref(py));
1005+
let locals2 = locals.clone_ref();
10061006

10071007
if let Err(e) = R::spawn_local(async move {
10081008
let result = R::scope_local(
1009-
Python::attach(|py| locals2.clone_ref(py)),
1009+
locals2.clone_ref(),
10101010
Cancellable::new_with_cancel_rx(fut, cancel_rx),
10111011
)
10121012
.await;
@@ -1510,7 +1510,7 @@ impl SenderGlue {
15101510
self.tx
15111511
.lock()
15121512
.unwrap()
1513-
.send(py, self.locals.clone_ref(py), item)
1513+
.send(py, self.locals.clone_ref(), item)
15141514
})
15151515
}
15161516
pub fn close(&mut self) -> PyResult<()> {

src/lib.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ pub mod doc_test {
395395
}
396396

397397
use std::future::Future;
398+
use std::sync::Arc;
398399

399400
use futures::channel::oneshot;
400401
use pyo3::{call::PyCallArgs, prelude::*, sync::PyOnceLock, types::PyDict};
@@ -472,17 +473,17 @@ fn copy_context(py: Python) -> PyResult<Bound<PyAny>> {
472473
#[derive(Debug)]
473474
pub struct TaskLocals {
474475
/// Track the event loop of the Python task
475-
event_loop: Py<PyAny>,
476+
event_loop: Arc<Py<PyAny>>,
476477
/// Track the contextvars of the Python task
477-
context: Py<PyAny>,
478+
context: Arc<Py<PyAny>>,
478479
}
479480

480481
impl TaskLocals {
481482
/// At a minimum, TaskLocals must store the event loop.
482483
pub fn new(event_loop: Bound<PyAny>) -> Self {
483484
Self {
484-
context: event_loop.py().None(),
485-
event_loop: event_loop.into(),
485+
context: Arc::new(event_loop.py().None()),
486+
event_loop: Arc::new(event_loop.into()),
486487
}
487488
}
488489

@@ -494,7 +495,7 @@ impl TaskLocals {
494495
/// Manually provide the contextvars for the current task.
495496
pub fn with_context(self, context: Bound<PyAny>) -> Self {
496497
Self {
497-
context: context.into(),
498+
context: Arc::new(context.into()),
498499
..self
499500
}
500501
}
@@ -516,10 +517,10 @@ impl TaskLocals {
516517

517518
/// Create a clone of the TaskLocals by incrementing the reference counters of the event loop and
518519
/// contextvars.
519-
pub fn clone_ref(&self, py: Python<'_>) -> Self {
520+
pub fn clone_ref(&self) -> Self {
520521
Self {
521-
event_loop: self.event_loop.clone_ref(py),
522-
context: self.context.clone_ref(py),
522+
event_loop: self.event_loop.clone(),
523+
context: self.context.clone(),
523524
}
524525
}
525526
}

src/tokio.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl ContextExt for TokioRuntime {
111111
TASK_LOCALS
112112
.try_with(|c| {
113113
c.get()
114-
.map(|locals| Python::attach(|py| locals.clone_ref(py)))
114+
.map(|locals| locals.clone_ref())
115115
})
116116
.unwrap_or_default()
117117
}

0 commit comments

Comments
 (0)