Skip to content

bpo-31350: Optimize get_event_loop and _get_running_loop #3347

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 10 commits into from
Sep 6, 2017
10 changes: 4 additions & 6 deletions Lib/asyncio/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,7 @@ 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
loop_pid = (None, None)


_running_loop = _RunningLoop()
Expand All @@ -619,8 +618,8 @@ def _get_running_loop():
This is a low-level function intended to be used by event loops.
This function is thread-specific.
"""
running_loop = _running_loop._loop
if running_loop is not None and _running_loop._pid == os.getpid():
running_loop, pid = _running_loop.loop_pid
if running_loop is not None and pid == os.getpid():
return running_loop


Expand All @@ -630,8 +629,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
_running_loop.loop_pid = (loop, os.getpid())


def _init_event_loop_policy():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Micro-optimize :func:`asyncio._get_running_loop` to become up to 10% faster.