bpo-22087: Fix Policy.get_event_loop() to detect fork (GH-7208)

Original patch by Dan O'Reilly.
(cherry picked from commit 5d97b7bcc1)

Co-authored-by: Yury Selivanov <yury@magic.io>
This commit is contained in:
Miss Islington (bot) 2018-05-29 14:09:17 -07:00 committed by GitHub
parent ca64f3ee5a
commit 2a7eb0b531
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 0 deletions

View file

@ -625,16 +625,23 @@ class BaseDefaultEventLoopPolicy(AbstractEventLoopPolicy):
class _Local(threading.local):
_loop = None
_pid = None
_set_called = False
def __init__(self):
self._local = self._Local()
self._local._pid = os.getpid()
def get_event_loop(self):
"""Get the event loop.
This may be None or an instance of EventLoop.
"""
if self._local._pid != os.getpid():
# If we detect we're in a child process forked by multiprocessing,
# we reset self._local so that we'll get a new event loop.
self._local = self._Local()
if (self._local._loop is None and
not self._local._set_called and
isinstance(threading.current_thread(), threading._MainThread)):