From d2f7090a591e3c5f0cd8b30c017df0a7d24bebf0 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 11 Oct 2023 16:04:43 +0100 Subject: [PATCH 1/8] GH-94597 add asyncio.EventLoop --- Doc/library/asyncio-eventloop.rst | 8 ++++++++ Doc/library/asyncio-runner.rst | 2 ++ Lib/asyncio/unix_events.py | 1 + Lib/asyncio/windows_events.py | 1 + Lib/test/test_asyncio/test_runners.py | 10 ++++++++++ 5 files changed, 22 insertions(+) diff --git a/Doc/library/asyncio-eventloop.rst b/Doc/library/asyncio-eventloop.rst index 04af53b980ff9e..90a21f797a5ecc 100644 --- a/Doc/library/asyncio-eventloop.rst +++ b/Doc/library/asyncio-eventloop.rst @@ -1723,6 +1723,14 @@ on Unix and :class:`ProactorEventLoop` on Windows. `MSDN documentation on I/O Completion Ports `_. +.. class:: EventLoop + + Uses the most effiecient available event loop for the given + platform. + + .. versionadded:: 3.13 + + .. availability:: Unix, Windows. .. class:: AbstractEventLoop diff --git a/Doc/library/asyncio-runner.rst b/Doc/library/asyncio-runner.rst index b68b2570ef071e..ec170dfde9e9aa 100644 --- a/Doc/library/asyncio-runner.rst +++ b/Doc/library/asyncio-runner.rst @@ -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 diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py index 28cef964debd36..7ac6c7a8968e62 100644 --- a/Lib/asyncio/unix_events.py +++ b/Lib/asyncio/unix_events.py @@ -1510,3 +1510,4 @@ def set_child_watcher(self, watcher): SelectorEventLoop = _UnixSelectorEventLoop DefaultEventLoopPolicy = _UnixDefaultEventLoopPolicy +EventLoop = SelectorEventLoop diff --git a/Lib/asyncio/windows_events.py b/Lib/asyncio/windows_events.py index c9a5fb841cb134..25727d7a55c5d5 100644 --- a/Lib/asyncio/windows_events.py +++ b/Lib/asyncio/windows_events.py @@ -894,3 +894,4 @@ class WindowsProactorEventLoopPolicy(events.BaseDefaultEventLoopPolicy): DefaultEventLoopPolicy = WindowsProactorEventLoopPolicy +EventLoop = ProactorEventLoop diff --git a/Lib/test/test_asyncio/test_runners.py b/Lib/test/test_asyncio/test_runners.py index 1eb5641914f2a4..2db1b6924fc235 100644 --- a/Lib/test/test_asyncio/test_runners.py +++ b/Lib/test/test_asyncio/test_runners.py @@ -272,6 +272,16 @@ async def main(): 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): From 47ea029e440752f8ac89bda8321570e58ea541f1 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 11 Oct 2023 15:07:26 +0000 Subject: [PATCH 2/8] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-10-11-15-07-21.gh-issue-94597.NbPC8t.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-10-11-15-07-21.gh-issue-94597.NbPC8t.rst 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 index 00000000000000..6ad922277a37a6 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-10-11-15-07-21.gh-issue-94597.NbPC8t.rst @@ -0,0 +1 @@ +Added :class:`asyncio.EventLoop` for use with the :func:`asyncio.run` *loop_factory* kwarg to avoid calling the asyncio policy system From dc9cfeef46f95451e358c71ccc2c1a2b56724343 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 11 Oct 2023 16:07:57 +0100 Subject: [PATCH 3/8] Update Doc/library/asyncio-eventloop.rst --- Doc/library/asyncio-eventloop.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/asyncio-eventloop.rst b/Doc/library/asyncio-eventloop.rst index 90a21f797a5ecc..98aa34db0b1ec7 100644 --- a/Doc/library/asyncio-eventloop.rst +++ b/Doc/library/asyncio-eventloop.rst @@ -1725,7 +1725,7 @@ on Unix and :class:`ProactorEventLoop` on Windows. .. class:: EventLoop - Uses the most effiecient available event loop for the given + Uses the most efficient available event loop for the given platform. .. versionadded:: 3.13 From 2beab6e030b30b6774f60a508538d03c8c8a44a2 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 11 Oct 2023 16:14:32 +0100 Subject: [PATCH 4/8] add EventLoop to relevant __all__s --- Lib/asyncio/unix_events.py | 1 + Lib/asyncio/windows_events.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py index 7ac6c7a8968e62..65f0923264d14e 100644 --- a/Lib/asyncio/unix_events.py +++ b/Lib/asyncio/unix_events.py @@ -32,6 +32,7 @@ 'FastChildWatcher', 'PidfdChildWatcher', 'MultiLoopChildWatcher', 'ThreadedChildWatcher', 'DefaultEventLoopPolicy', + 'EventLoop', ) diff --git a/Lib/asyncio/windows_events.py b/Lib/asyncio/windows_events.py index 25727d7a55c5d5..4a4c4bea8948a0 100644 --- a/Lib/asyncio/windows_events.py +++ b/Lib/asyncio/windows_events.py @@ -29,7 +29,7 @@ __all__ = ( 'SelectorEventLoop', 'ProactorEventLoop', 'IocpProactor', 'DefaultEventLoopPolicy', 'WindowsSelectorEventLoopPolicy', - 'WindowsProactorEventLoopPolicy', + 'WindowsProactorEventLoopPolicy', 'EventLoop', ) From 6290284460535bdc89685c5648ef19e0f485fc98 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 11 Oct 2023 16:46:47 +0100 Subject: [PATCH 5/8] add missing sys import --- Lib/test/test_asyncio/test_runners.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_asyncio/test_runners.py b/Lib/test/test_asyncio/test_runners.py index 2db1b6924fc235..13493d3c806d6a 100644 --- a/Lib/test/test_asyncio/test_runners.py +++ b/Lib/test/test_asyncio/test_runners.py @@ -3,6 +3,7 @@ import contextvars import re import signal +import sys import threading import unittest from test.test_asyncio import utils as test_utils From ad2c22ca53ddde015b1da1c2fec2c1adee18ccfc Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 12 Oct 2023 11:06:08 +0100 Subject: [PATCH 6/8] Update Misc/NEWS.d/next/Library/2023-10-11-15-07-21.gh-issue-94597.NbPC8t.rst Co-authored-by: Guido van Rossum --- .../next/Library/2023-10-11-15-07-21.gh-issue-94597.NbPC8t.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 6ad922277a37a6..6874c6111c3f2c 100644 --- 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 @@ -1 +1 @@ -Added :class:`asyncio.EventLoop` for use with the :func:`asyncio.run` *loop_factory* kwarg to avoid calling the asyncio policy system +Added :class:`asyncio.EventLoop` for use with the :func:`asyncio.run` *loop_factory* kwarg to avoid calling the asyncio policy system. From 27d96caf249d08ad8e7752e511aeef14b28b5f0f Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 12 Oct 2023 11:30:10 +0100 Subject: [PATCH 7/8] asyncio.EventLoop docs clarification --- Doc/library/asyncio-eventloop.rst | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Doc/library/asyncio-eventloop.rst b/Doc/library/asyncio-eventloop.rst index 98aa34db0b1ec7..09ccff1c689f1b 100644 --- a/Doc/library/asyncio-eventloop.rst +++ b/Doc/library/asyncio-eventloop.rst @@ -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. @@ -1725,9 +1725,11 @@ on Unix and :class:`ProactorEventLoop` on Windows. .. class:: EventLoop - Uses the most efficient available event loop for the given + 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 .. availability:: Unix, Windows. From afaecf2eb9011e7a67bc094da30904e8937aaaa3 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 12 Oct 2023 11:52:34 +0100 Subject: [PATCH 8/8] remove .. availability tag from asyncio.EventLoop it's always available --- Doc/library/asyncio-eventloop.rst | 2 -- 1 file changed, 2 deletions(-) diff --git a/Doc/library/asyncio-eventloop.rst b/Doc/library/asyncio-eventloop.rst index 09ccff1c689f1b..361e7bb9c8f2fa 100644 --- a/Doc/library/asyncio-eventloop.rst +++ b/Doc/library/asyncio-eventloop.rst @@ -1732,8 +1732,6 @@ By default asyncio is configured to use :class:`EventLoop`. .. versionadded:: 3.13 - .. availability:: Unix, Windows. - .. class:: AbstractEventLoop Abstract base class for asyncio-compliant event loops.