@@ -395,6 +395,7 @@ pub mod doc_test {
395395}
396396
397397use std:: future:: Future ;
398+ use std:: sync:: Arc ;
398399
399400use futures:: channel:: oneshot;
400401use pyo3:: { call:: PyCallArgs , prelude:: * , sync:: PyOnceLock , types:: PyDict } ;
@@ -472,17 +473,17 @@ fn copy_context(py: Python) -> PyResult<Bound<PyAny>> {
472473#[ derive( Debug ) ]
473474pub 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
480481impl 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}
0 commit comments