Commit abae67eb authored by Yury Selivanov's avatar Yury Selivanov Committed by GitHub

Add asyncio.get_running_loop() function. (#4782)

parent 3e975181
...@@ -25,6 +25,13 @@ the execution of the process. ...@@ -25,6 +25,13 @@ the execution of the process.
Equivalent to calling ``get_event_loop_policy().new_event_loop()``. Equivalent to calling ``get_event_loop_policy().new_event_loop()``.
.. function:: get_running_loop()
Return the running event loop in the current OS thread. If there
is no running event loop a :exc:`RuntimeError` is raised.
.. versionadded:: 3.7
.. _asyncio-event-loops: .. _asyncio-event-loops:
......
...@@ -7,7 +7,8 @@ __all__ = ( ...@@ -7,7 +7,8 @@ __all__ = (
'get_event_loop_policy', 'set_event_loop_policy', 'get_event_loop_policy', 'set_event_loop_policy',
'get_event_loop', 'set_event_loop', 'new_event_loop', 'get_event_loop', 'set_event_loop', 'new_event_loop',
'get_child_watcher', 'set_child_watcher', 'get_child_watcher', 'set_child_watcher',
'_set_running_loop', '_get_running_loop', '_set_running_loop', 'get_running_loop',
'_get_running_loop',
) )
import functools import functools
...@@ -646,6 +647,17 @@ class _RunningLoop(threading.local): ...@@ -646,6 +647,17 @@ class _RunningLoop(threading.local):
_running_loop = _RunningLoop() _running_loop = _RunningLoop()
def get_running_loop():
"""Return the running event loop. Raise a RuntimeError if there is none.
This function is thread-specific.
"""
loop = _get_running_loop()
if loop is None:
raise RuntimeError('no running event loop')
return loop
def _get_running_loop(): def _get_running_loop():
"""Return the running event loop or None. """Return the running event loop or None.
......
...@@ -2733,10 +2733,13 @@ class PolicyTests(unittest.TestCase): ...@@ -2733,10 +2733,13 @@ class PolicyTests(unittest.TestCase):
try: try:
asyncio.set_event_loop_policy(Policy()) asyncio.set_event_loop_policy(Policy())
loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()
with self.assertRaisesRegex(RuntimeError, 'no running'):
self.assertIs(asyncio.get_running_loop(), None)
self.assertIs(asyncio._get_running_loop(), None) self.assertIs(asyncio._get_running_loop(), None)
async def func(): async def func():
self.assertIs(asyncio.get_event_loop(), loop) self.assertIs(asyncio.get_event_loop(), loop)
self.assertIs(asyncio.get_running_loop(), loop)
self.assertIs(asyncio._get_running_loop(), loop) self.assertIs(asyncio._get_running_loop(), loop)
loop.run_until_complete(func()) loop.run_until_complete(func())
...@@ -2745,6 +2748,9 @@ class PolicyTests(unittest.TestCase): ...@@ -2745,6 +2748,9 @@ class PolicyTests(unittest.TestCase):
if loop is not None: if loop is not None:
loop.close() loop.close()
with self.assertRaisesRegex(RuntimeError, 'no running'):
self.assertIs(asyncio.get_running_loop(), None)
self.assertIs(asyncio._get_running_loop(), None) self.assertIs(asyncio._get_running_loop(), None)
......
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