Skip to content

bpo-29703: asyncio: Fix creating new event loops in child processes. #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Lib/asyncio/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import functools
import inspect
import os
import reprlib
import socket
import subprocess
Expand Down Expand Up @@ -611,6 +612,9 @@ def new_event_loop(self):
# A TLS for the running event loop, used by _get_running_loop.
class _RunningLoop(threading.local):
_loop = None
_pid = None


_running_loop = _RunningLoop()


Expand All @@ -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):
Expand All @@ -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


Expand Down
5 changes: 4 additions & 1 deletion Lib/asyncio/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,15 @@ def new_test_loop(self, gen=None):
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)

Expand Down
22 changes: 22 additions & 0 deletions Lib/test/test_asyncio/test_events.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Tests for events.py."""

import collections.abc
import concurrent.futures
import functools
import gc
import io
Expand Down Expand Up @@ -57,6 +58,15 @@ def osx_tiger():
return version < (10, 5)


def _test_get_event_loop_new_process__sub_proc():
async def doit():
return 'hello'

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return loop.run_until_complete(doit())


ONLYCERT = data_file('ssl_cert.pem')
ONLYKEY = data_file('ssl_key.pem')
SIGNED_CERTFILE = data_file('keycert3.pem')
Expand Down Expand Up @@ -2181,6 +2191,18 @@ def tearDown(self):
asyncio.set_child_watcher(None)
super().tearDown()

def test_get_event_loop_new_process(self):
async def main():
pool = concurrent.futures.ProcessPoolExecutor()
return await self.loop.run_in_executor(
pool, _test_get_event_loop_new_process__sub_proc)

self.unpatch_get_running_loop()

self.assertEqual(
self.loop.run_until_complete(main()),
'hello')

if hasattr(selectors, 'KqueueSelector'):
class KqueueEventLoopTests(UnixEventLoopTestsMixin,
SubprocessTestsMixin,
Expand Down
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ Extension Modules
Library
-------

- bpo-29703: Fix asyncio to support instantiation of new event loops
in child processes.

- bpo-29615: SimpleXMLRPCDispatcher no longer chains KeyError (or any other
exception) to exception(s) raised in the dispatched methods.
Patch by Petr Motejlek.
Expand Down