File tree 2 files changed +17
-6
lines changed
2 files changed +17
-6
lines changed Original file line number Diff line number Diff line change @@ -628,20 +628,25 @@ class _Local(threading.local):
628
628
_pid = None
629
629
_set_called = False
630
630
631
+ def __init__ (self ):
632
+ super ().__init__ ()
633
+ self ._pid = os .getpid ()
634
+
631
635
def __init__ (self ):
632
636
self ._local = self ._Local ()
633
- self ._local ._pid = os .getpid ()
634
-
635
- def get_event_loop (self ):
636
- """Get the event loop.
637
637
638
- This may be None or an instance of EventLoop.
639
- """
638
+ def _check_pid (self ):
640
639
if self ._local ._pid != os .getpid ():
641
640
# If we detect we're in a child process forked by multiprocessing,
642
641
# we reset self._local so that we'll get a new event loop.
643
642
self ._local = self ._Local ()
644
643
644
+ def get_event_loop (self ):
645
+ """Get the event loop.
646
+
647
+ This may be None or an instance of EventLoop.
648
+ """
649
+ self ._check_pid ()
645
650
if (self ._local ._loop is None and
646
651
not self ._local ._set_called and
647
652
isinstance (threading .current_thread (), threading ._MainThread )):
@@ -655,6 +660,7 @@ def get_event_loop(self):
655
660
656
661
def set_event_loop (self , loop ):
657
662
"""Set the event loop."""
663
+ self ._check_pid ()
658
664
self ._local ._set_called = True
659
665
assert loop is None or isinstance (loop , AbstractEventLoop )
660
666
self ._local ._loop = loop
Original file line number Diff line number Diff line change @@ -1874,8 +1874,11 @@ def test_get_child_watcher_with_mainloop_existing(self):
1874
1874
loop .close ()
1875
1875
1876
1876
def test_get_child_watcher_thread (self ):
1877
+ success = False
1877
1878
1878
1879
def f ():
1880
+ nonlocal success
1881
+
1879
1882
policy .set_event_loop (policy .new_event_loop ())
1880
1883
1881
1884
self .assertIsInstance (policy .get_event_loop (),
@@ -1886,12 +1889,14 @@ def f():
1886
1889
self .assertIsNone (watcher ._loop )
1887
1890
1888
1891
policy .get_event_loop ().close ()
1892
+ success = True
1889
1893
1890
1894
policy = self .create_policy ()
1891
1895
1892
1896
th = threading .Thread (target = f )
1893
1897
th .start ()
1894
1898
th .join ()
1899
+ self .assertTrue (success )
1895
1900
1896
1901
def test_child_watcher_replace_mainloop_existing (self ):
1897
1902
policy = self .create_policy ()
You can’t perform that action at this time.
0 commit comments