summaryrefslogtreecommitdiff
path: root/Lib/asyncio
diff options
context:
space:
mode:
authorYury Selivanov <yselivanov@gmail.com>2017-03-02 20:07:11 -0500
committerGitHub <noreply@github.com>2017-03-02 20:07:11 -0500
commitba7e1f9a4e06c0b4ad594fd64edcaf7292515820 (patch)
tree97e42c3e5ec67118d5953fcf3c86a0254340bcd1 /Lib/asyncio
parentcdf037c212675b11c10c4d0d76496f5d81154882 (diff)
downloadcpython-git-ba7e1f9a4e06c0b4ad594fd64edcaf7292515820.tar.gz
bpo-29703: asyncio: Fix creating new event loops in child processes. (#404)
Diffstat (limited to 'Lib/asyncio')
-rw-r--r--Lib/asyncio/events.py8
-rw-r--r--Lib/asyncio/test_utils.py5
2 files changed, 11 insertions, 2 deletions
diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py
index 28a45fc3cc..7b30b4c84a 100644
--- a/Lib/asyncio/events.py
+++ b/Lib/asyncio/events.py
@@ -11,6 +11,7 @@ __all__ = ['AbstractEventLoopPolicy',
import functools
import inspect
+import os
import reprlib
import socket
import subprocess
@@ -611,6 +612,9 @@ _lock = threading.Lock()
# A TLS for the running event loop, used by _get_running_loop.
class _RunningLoop(threading.local):
_loop = None
+ _pid = None
+
+
_running_loop = _RunningLoop()
@@ -620,7 +624,8 @@ def _get_running_loop():
This is a low-level function intended to be used by event loops.
This function is thread-specific.
"""
- return _running_loop._loop
+ if _running_loop._pid == os.getpid():
+ return _running_loop._loop
def _set_running_loop(loop):
@@ -629,6 +634,7 @@ def _set_running_loop(loop):
This is a low-level function intended to be used by event loops.
This function is thread-specific.
"""
+ _running_loop._pid = os.getpid()
_running_loop._loop = loop
diff --git a/Lib/asyncio/test_utils.py b/Lib/asyncio/test_utils.py
index 99e3839f45..b12d5db2a9 100644
--- a/Lib/asyncio/test_utils.py
+++ b/Lib/asyncio/test_utils.py
@@ -449,12 +449,15 @@ class TestCase(unittest.TestCase):
self.set_event_loop(loop)
return loop
+ def unpatch_get_running_loop(self):
+ events._get_running_loop = self._get_running_loop
+
def setUp(self):
self._get_running_loop = events._get_running_loop
events._get_running_loop = lambda: None
def tearDown(self):
- events._get_running_loop = self._get_running_loop
+ self.unpatch_get_running_loop()
events.set_event_loop(None)