]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-94597: Add asyncio.EventLoop (#110723)
authorThomas Grainger <tagrain@gmail.com>
Thu, 12 Oct 2023 14:13:57 +0000 (07:13 -0700)
committerGitHub <noreply@github.com>
Thu, 12 Oct 2023 14:13:57 +0000 (07:13 -0700)
This is needed to pave the way for deprecating and eventually killing the event loop policy system (which is over-engineered and rarely used).

Doc/library/asyncio-eventloop.rst
Doc/library/asyncio-runner.rst
Lib/asyncio/unix_events.py
Lib/asyncio/windows_events.py
Lib/test/test_asyncio/test_runners.py
Misc/NEWS.d/next/Library/2023-10-11-15-07-21.gh-issue-94597.NbPC8t.rst [new file with mode: 0644]

index 04af53b980ff9eb90adeb4782d390c869fe9aa9a..361e7bb9c8f2fa8f7a8e572faf69624369df4f6d 100644 (file)
@@ -1686,13 +1686,13 @@ Event Loop Implementations
 asyncio ships with two different event loop implementations:
 :class:`SelectorEventLoop` and :class:`ProactorEventLoop`.
 
-By default asyncio is configured to use :class:`SelectorEventLoop`
-on Unix and :class:`ProactorEventLoop` on Windows.
+By default asyncio is configured to use :class:`EventLoop`.
 
 
 .. class:: SelectorEventLoop
 
-   An event loop based on the :mod:`selectors` module.
+   A subclass of :class:`AbstractEventLoop` based on the
+   :mod:`selectors` module.
 
    Uses the most efficient *selector* available for the given
    platform.  It is also possible to manually configure the
@@ -1714,7 +1714,7 @@ on Unix and :class:`ProactorEventLoop` on Windows.
 
 .. class:: ProactorEventLoop
 
-   An event loop for Windows that uses "I/O Completion Ports" (IOCP).
+   A subclass of :class:`AbstractEventLoop` for Windows that uses "I/O Completion Ports" (IOCP).
 
    .. availability:: Windows.
 
@@ -1723,6 +1723,14 @@ on Unix and :class:`ProactorEventLoop` on Windows.
       `MSDN documentation on I/O Completion Ports
       <https://docs.microsoft.com/en-ca/windows/desktop/FileIO/i-o-completion-ports>`_.
 
+.. class:: EventLoop
+
+    An alias to the most efficient available subclass of :class:`AbstractEventLoop` for the given
+    platform.
+
+    It is an alias to :class:`SelectorEventLoop` on Unix and :class:`ProactorEventLoop` on Windows.
+
+   .. versionadded:: 3.13
 
 .. class:: AbstractEventLoop
 
index b68b2570ef071e14b2a0084abacc3f3d2e361e3a..ec170dfde9e9aa2db3c93c46dde184835eb0029b 100644 (file)
@@ -42,6 +42,8 @@ Running an asyncio Program
    This function should be used as a main entry point for asyncio programs,
    and should ideally only be called once. It is recommended to use
    *loop_factory* to configure the event loop instead of policies.
+   Passing :class:`asyncio.EventLoop` allows running asyncio without the
+   policy system.
 
    The executor is given a timeout duration of 5 minutes to shutdown.
    If the executor hasn't finished within that duration, a warning is
index 28cef964debd3698afd6f438ceff806e57c70b6e..65f0923264d14efa3dfe16a19dc4b69c054bfbef 100644 (file)
@@ -32,6 +32,7 @@ __all__ = (
     'FastChildWatcher', 'PidfdChildWatcher',
     'MultiLoopChildWatcher', 'ThreadedChildWatcher',
     'DefaultEventLoopPolicy',
+    'EventLoop',
 )
 
 
@@ -1510,3 +1511,4 @@ class _UnixDefaultEventLoopPolicy(events.BaseDefaultEventLoopPolicy):
 
 SelectorEventLoop = _UnixSelectorEventLoop
 DefaultEventLoopPolicy = _UnixDefaultEventLoopPolicy
+EventLoop = SelectorEventLoop
index c9a5fb841cb134ba61165073e83f896dcf8d4dab..4a4c4bea8948a0e1635c55507710bfbd132c9664 100644 (file)
@@ -29,7 +29,7 @@ from .log import logger
 __all__ = (
     'SelectorEventLoop', 'ProactorEventLoop', 'IocpProactor',
     'DefaultEventLoopPolicy', 'WindowsSelectorEventLoopPolicy',
-    'WindowsProactorEventLoopPolicy',
+    'WindowsProactorEventLoopPolicy', 'EventLoop',
 )
 
 
@@ -894,3 +894,4 @@ class WindowsProactorEventLoopPolicy(events.BaseDefaultEventLoopPolicy):
 
 
 DefaultEventLoopPolicy = WindowsProactorEventLoopPolicy
+EventLoop = ProactorEventLoop
index 1eb5641914f2a4c81ce1630521225af17178661c..13493d3c806d6a11b8256f5dea684a652b78243f 100644 (file)
@@ -3,6 +3,7 @@ import asyncio
 import contextvars
 import re
 import signal
+import sys
 import threading
 import unittest
 from test.test_asyncio import utils as test_utils
@@ -272,6 +273,16 @@ class RunTests(BaseTest):
         asyncio.run(main(), loop_factory=factory)
         factory.assert_called_once_with()
 
+    def test_loop_factory_default_event_loop(self):
+        async def main():
+            if sys.platform == "win32":
+                self.assertIsInstance(asyncio.get_running_loop(), asyncio.ProactorEventLoop)
+            else:
+                self.assertIsInstance(asyncio.get_running_loop(), asyncio.SelectorEventLoop)
+
+
+        asyncio.run(main(), loop_factory=asyncio.EventLoop)
+
 
 class RunnerTests(BaseTest):
 
diff --git a/Misc/NEWS.d/next/Library/2023-10-11-15-07-21.gh-issue-94597.NbPC8t.rst b/Misc/NEWS.d/next/Library/2023-10-11-15-07-21.gh-issue-94597.NbPC8t.rst
new file mode 100644 (file)
index 0000000..6874c61
--- /dev/null
@@ -0,0 +1 @@
+Added :class:`asyncio.EventLoop` for use with the :func:`asyncio.run` *loop_factory* kwarg to avoid calling the asyncio policy system.