]> granicus.if.org Git - python/commitdiff
Add asyncio.get_running_loop() function. (#4782)
authorYury Selivanov <yury@magic.io>
Mon, 11 Dec 2017 15:07:44 +0000 (10:07 -0500)
committerGitHub <noreply@github.com>
Mon, 11 Dec 2017 15:07:44 +0000 (10:07 -0500)
Doc/library/asyncio-eventloops.rst
Lib/asyncio/events.py
Lib/test/test_asyncio/test_events.py
Misc/NEWS.d/next/Library/2017-12-10-12-30-13.bpo-32269.Q85pKj.rst [new file with mode: 0644]

index 2097260e2190fe2b921473f2c335432eebc82b2b..3051fde5b93baf93c53e5f48012adf4e365e6c44 100644 (file)
@@ -25,6 +25,13 @@ the execution of the process.
 
    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:
 
index 7db1ded8e8b379eccf1989afd5ceaa9ca7058bed..e425b06e42fa3692cf4b32230661bc19f41f64eb 100644 (file)
@@ -7,7 +7,8 @@ __all__ = (
     'get_event_loop_policy', 'set_event_loop_policy',
     'get_event_loop', 'set_event_loop', 'new_event_loop',
     'get_child_watcher', 'set_child_watcher',
-    '_set_running_loop', '_get_running_loop',
+    '_set_running_loop', 'get_running_loop',
+    '_get_running_loop',
 )
 
 import functools
@@ -646,6 +647,17 @@ class _RunningLoop(threading.local):
 _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():
     """Return the running event loop or None.
 
index 39d5bb54a9d64914353926da686ad3030cd3924d..1315febe403500e15be31b54e0f44a6f3f5166e5 100644 (file)
@@ -2733,10 +2733,13 @@ class PolicyTests(unittest.TestCase):
         try:
             asyncio.set_event_loop_policy(Policy())
             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)
 
             async def func():
                 self.assertIs(asyncio.get_event_loop(), loop)
+                self.assertIs(asyncio.get_running_loop(), loop)
                 self.assertIs(asyncio._get_running_loop(), loop)
 
             loop.run_until_complete(func())
@@ -2745,6 +2748,9 @@ class PolicyTests(unittest.TestCase):
             if loop is not None:
                 loop.close()
 
+        with self.assertRaisesRegex(RuntimeError, 'no running'):
+            self.assertIs(asyncio.get_running_loop(), None)
+
         self.assertIs(asyncio._get_running_loop(), None)
 
 
diff --git a/Misc/NEWS.d/next/Library/2017-12-10-12-30-13.bpo-32269.Q85pKj.rst b/Misc/NEWS.d/next/Library/2017-12-10-12-30-13.bpo-32269.Q85pKj.rst
new file mode 100644 (file)
index 0000000..9fa1244
--- /dev/null
@@ -0,0 +1 @@
+Add asyncio.get_running_loop() function.