Commit 80bbe6a7 authored by jimmylai's avatar jimmylai Committed by Yury Selivanov

bpo-31350: Optimize get_event_loop and _get_running_loop (#3347)

* 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
parent 305e56c2
...@@ -606,8 +606,7 @@ _lock = threading.Lock() ...@@ -606,8 +606,7 @@ _lock = threading.Lock()
# A TLS for the running event loop, used by _get_running_loop. # A TLS for the running event loop, used by _get_running_loop.
class _RunningLoop(threading.local): class _RunningLoop(threading.local):
_loop = None loop_pid = (None, None)
_pid = None
_running_loop = _RunningLoop() _running_loop = _RunningLoop()
...@@ -619,8 +618,8 @@ def _get_running_loop(): ...@@ -619,8 +618,8 @@ def _get_running_loop():
This is a low-level function intended to be used by event loops. This is a low-level function intended to be used by event loops.
This function is thread-specific. This function is thread-specific.
""" """
running_loop = _running_loop._loop running_loop, pid = _running_loop.loop_pid
if running_loop is not None and _running_loop._pid == os.getpid(): if running_loop is not None and pid == os.getpid():
return running_loop return running_loop
...@@ -630,8 +629,7 @@ def _set_running_loop(loop): ...@@ -630,8 +629,7 @@ def _set_running_loop(loop):
This is a low-level function intended to be used by event loops. This is a low-level function intended to be used by event loops.
This function is thread-specific. This function is thread-specific.
""" """
_running_loop._pid = os.getpid() _running_loop.loop_pid = (loop, os.getpid())
_running_loop._loop = loop
def _init_event_loop_policy(): def _init_event_loop_policy():
......
Micro-optimize :func:`asyncio._get_running_loop` to become up to 10% faster.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment