Skip to content

Commit ff125e1

Browse files
miss-islingtonMariatta
authored andcommitted
bpo-31350: Optimize get_event_loop and _get_running_loop (GH-3347) (GH-3373)
* call remove_done_callback in finally section * Optimize get_event_loop and _get_running_loop * rename _loop_pid as loop_pid and add blurb news * rename _loop_pid as loop_pid and add blurb news * add back _RunningLoop * Update 2017-09-05-10-30-48.bpo-31350.dXJ-7N.rst * Update 2017-09-05-10-30-48.bpo-31350.dXJ-7N.rst (cherry picked from commit 80bbe6a)
1 parent 1145352 commit ff125e1

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

Lib/asyncio/events.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -611,8 +611,7 @@ def new_event_loop(self):
611611

612612
# A TLS for the running event loop, used by _get_running_loop.
613613
class _RunningLoop(threading.local):
614-
_loop = None
615-
_pid = None
614+
loop_pid = (None, None)
616615

617616

618617
_running_loop = _RunningLoop()
@@ -624,8 +623,8 @@ def _get_running_loop():
624623
This is a low-level function intended to be used by event loops.
625624
This function is thread-specific.
626625
"""
627-
running_loop = _running_loop._loop
628-
if running_loop is not None and _running_loop._pid == os.getpid():
626+
running_loop, pid = _running_loop.loop_pid
627+
if running_loop is not None and pid == os.getpid():
629628
return running_loop
630629

631630

@@ -635,8 +634,7 @@ def _set_running_loop(loop):
635634
This is a low-level function intended to be used by event loops.
636635
This function is thread-specific.
637636
"""
638-
_running_loop._pid = os.getpid()
639-
_running_loop._loop = loop
637+
_running_loop.loop_pid = (loop, os.getpid())
640638

641639

642640
def _init_event_loop_policy():
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Micro-optimize :func:`asyncio._get_running_loop` to become up to 10% faster.

0 commit comments

Comments
 (0)