[Python-checkins] bpo-31350: Optimize get_event_loop and _get_running_loop (GH-3347) (GH-3373)

Mariatta webhook-mailer at python.org
Tue Sep 5 23:05:38 EDT 2017


https://github.com/python/cpython/commit/ff125e1aa9ee4eb928de79320a0e7c1b0c0f58f4
commit: ff125e1aa9ee4eb928de79320a0e7c1b0c0f58f4
branch: 3.6
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Mariatta <Mariatta at users.noreply.github.com>
date: 2017-09-05T20:05:35-07:00
summary:

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 80bbe6a7b67f33d0d0976bb8e3e5ba26b6b0e626)

files:
A Misc/NEWS.d/next/Library/2017-09-05-10-30-48.bpo-31350.dXJ-7N.rst
M Lib/asyncio/events.py

diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py
index e85634e588f..d41f3f5b755 100644
--- a/Lib/asyncio/events.py
+++ b/Lib/asyncio/events.py
@@ -611,8 +611,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()
@@ -624,8 +623,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
 
 
@@ -635,8 +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
+    _running_loop.loop_pid = (loop, os.getpid())
 
 
 def _init_event_loop_policy():
diff --git a/Misc/NEWS.d/next/Library/2017-09-05-10-30-48.bpo-31350.dXJ-7N.rst b/Misc/NEWS.d/next/Library/2017-09-05-10-30-48.bpo-31350.dXJ-7N.rst
new file mode 100644
index 00000000000..299cf3c9d54
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2017-09-05-10-30-48.bpo-31350.dXJ-7N.rst
@@ -0,0 +1 @@
+Micro-optimize :func:`asyncio._get_running_loop` to become up to 10% faster.



More information about the Python-checkins mailing list