File tree 4 files changed +36
-2
lines changed
4 files changed +36
-2
lines changed Original file line number Diff line number Diff line change 11
11
12
12
import functools
13
13
import inspect
14
+ import os
14
15
import reprlib
15
16
import socket
16
17
import subprocess
@@ -611,6 +612,9 @@ def new_event_loop(self):
611
612
# A TLS for the running event loop, used by _get_running_loop.
612
613
class _RunningLoop (threading .local ):
613
614
_loop = None
615
+ _pid = None
616
+
617
+
614
618
_running_loop = _RunningLoop ()
615
619
616
620
@@ -620,7 +624,8 @@ def _get_running_loop():
620
624
This is a low-level function intended to be used by event loops.
621
625
This function is thread-specific.
622
626
"""
623
- return _running_loop ._loop
627
+ if _running_loop ._pid == os .getpid ():
628
+ return _running_loop ._loop
624
629
625
630
626
631
def _set_running_loop (loop ):
@@ -629,6 +634,7 @@ def _set_running_loop(loop):
629
634
This is a low-level function intended to be used by event loops.
630
635
This function is thread-specific.
631
636
"""
637
+ _running_loop ._pid = os .getpid ()
632
638
_running_loop ._loop = loop
633
639
634
640
Original file line number Diff line number Diff line change @@ -449,12 +449,15 @@ def new_test_loop(self, gen=None):
449
449
self .set_event_loop (loop )
450
450
return loop
451
451
452
+ def unpatch_get_running_loop (self ):
453
+ events ._get_running_loop = self ._get_running_loop
454
+
452
455
def setUp (self ):
453
456
self ._get_running_loop = events ._get_running_loop
454
457
events ._get_running_loop = lambda : None
455
458
456
459
def tearDown (self ):
457
- events . _get_running_loop = self ._get_running_loop
460
+ self .unpatch_get_running_loop ()
458
461
459
462
events .set_event_loop (None )
460
463
Original file line number Diff line number Diff line change 1
1
"""Tests for events.py."""
2
2
3
3
import collections .abc
4
+ import concurrent .futures
4
5
import functools
5
6
import gc
6
7
import io
@@ -57,6 +58,15 @@ def osx_tiger():
57
58
return version < (10 , 5 )
58
59
59
60
61
+ def _test_get_event_loop_new_process__sub_proc ():
62
+ async def doit ():
63
+ return 'hello'
64
+
65
+ loop = asyncio .new_event_loop ()
66
+ asyncio .set_event_loop (loop )
67
+ return loop .run_until_complete (doit ())
68
+
69
+
60
70
ONLYCERT = data_file ('ssl_cert.pem' )
61
71
ONLYKEY = data_file ('ssl_key.pem' )
62
72
SIGNED_CERTFILE = data_file ('keycert3.pem' )
@@ -2181,6 +2191,18 @@ def tearDown(self):
2181
2191
asyncio .set_child_watcher (None )
2182
2192
super ().tearDown ()
2183
2193
2194
+ def test_get_event_loop_new_process (self ):
2195
+ async def main ():
2196
+ pool = concurrent .futures .ProcessPoolExecutor ()
2197
+ return await self .loop .run_in_executor (
2198
+ pool , _test_get_event_loop_new_process__sub_proc )
2199
+
2200
+ self .unpatch_get_running_loop ()
2201
+
2202
+ self .assertEqual (
2203
+ self .loop .run_until_complete (main ()),
2204
+ 'hello' )
2205
+
2184
2206
if hasattr (selectors , 'KqueueSelector' ):
2185
2207
class KqueueEventLoopTests (UnixEventLoopTestsMixin ,
2186
2208
SubprocessTestsMixin ,
Original file line number Diff line number Diff line change @@ -688,6 +688,9 @@ Library
688
688
- Issue #24142: Reading a corrupt config file left configparser in an
689
689
invalid state. Original patch by Florian Höch.
690
690
691
+ - Issue #29703: Fix asyncio to support instantiation of new event loops
692
+ in child processes.
693
+
691
694
Windows
692
695
-------
693
696
You can’t perform that action at this time.
0 commit comments