Skip to content

Commit 8c6c14b

Browse files
authored
gh-94597: Add asyncio.EventLoop (#110723)
This is needed to pave the way for deprecating and eventually killing the event loop policy system (which is over-engineered and rarely used).
1 parent 1e3460d commit 8c6c14b

File tree

6 files changed

+30
-5
lines changed

6 files changed

+30
-5
lines changed

Doc/library/asyncio-eventloop.rst

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,13 +1686,13 @@ Event Loop Implementations
16861686
asyncio ships with two different event loop implementations:
16871687
:class:`SelectorEventLoop` and :class:`ProactorEventLoop`.
16881688

1689-
By default asyncio is configured to use :class:`SelectorEventLoop`
1690-
on Unix and :class:`ProactorEventLoop` on Windows.
1689+
By default asyncio is configured to use :class:`EventLoop`.
16911690

16921691

16931692
.. class:: SelectorEventLoop
16941693

1695-
An event loop based on the :mod:`selectors` module.
1694+
A subclass of :class:`AbstractEventLoop` based on the
1695+
:mod:`selectors` module.
16961696

16971697
Uses the most efficient *selector* available for the given
16981698
platform. It is also possible to manually configure the
@@ -1714,7 +1714,7 @@ on Unix and :class:`ProactorEventLoop` on Windows.
17141714

17151715
.. class:: ProactorEventLoop
17161716

1717-
An event loop for Windows that uses "I/O Completion Ports" (IOCP).
1717+
A subclass of :class:`AbstractEventLoop` for Windows that uses "I/O Completion Ports" (IOCP).
17181718

17191719
.. availability:: Windows.
17201720

@@ -1723,6 +1723,14 @@ on Unix and :class:`ProactorEventLoop` on Windows.
17231723
`MSDN documentation on I/O Completion Ports
17241724
<https://docs.microsoft.com/en-ca/windows/desktop/FileIO/i-o-completion-ports>`_.
17251725

1726+
.. class:: EventLoop
1727+
1728+
An alias to the most efficient available subclass of :class:`AbstractEventLoop` for the given
1729+
platform.
1730+
1731+
It is an alias to :class:`SelectorEventLoop` on Unix and :class:`ProactorEventLoop` on Windows.
1732+
1733+
.. versionadded:: 3.13
17261734

17271735
.. class:: AbstractEventLoop
17281736

Doc/library/asyncio-runner.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Running an asyncio Program
4242
This function should be used as a main entry point for asyncio programs,
4343
and should ideally only be called once. It is recommended to use
4444
*loop_factory* to configure the event loop instead of policies.
45+
Passing :class:`asyncio.EventLoop` allows running asyncio without the
46+
policy system.
4547

4648
The executor is given a timeout duration of 5 minutes to shutdown.
4749
If the executor hasn't finished within that duration, a warning is

Lib/asyncio/unix_events.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
'FastChildWatcher', 'PidfdChildWatcher',
3333
'MultiLoopChildWatcher', 'ThreadedChildWatcher',
3434
'DefaultEventLoopPolicy',
35+
'EventLoop',
3536
)
3637

3738

@@ -1510,3 +1511,4 @@ def set_child_watcher(self, watcher):
15101511

15111512
SelectorEventLoop = _UnixSelectorEventLoop
15121513
DefaultEventLoopPolicy = _UnixDefaultEventLoopPolicy
1514+
EventLoop = SelectorEventLoop

Lib/asyncio/windows_events.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
__all__ = (
3030
'SelectorEventLoop', 'ProactorEventLoop', 'IocpProactor',
3131
'DefaultEventLoopPolicy', 'WindowsSelectorEventLoopPolicy',
32-
'WindowsProactorEventLoopPolicy',
32+
'WindowsProactorEventLoopPolicy', 'EventLoop',
3333
)
3434

3535

@@ -894,3 +894,4 @@ class WindowsProactorEventLoopPolicy(events.BaseDefaultEventLoopPolicy):
894894

895895

896896
DefaultEventLoopPolicy = WindowsProactorEventLoopPolicy
897+
EventLoop = ProactorEventLoop

Lib/test/test_asyncio/test_runners.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import contextvars
44
import re
55
import signal
6+
import sys
67
import threading
78
import unittest
89
from test.test_asyncio import utils as test_utils
@@ -272,6 +273,16 @@ async def main():
272273
asyncio.run(main(), loop_factory=factory)
273274
factory.assert_called_once_with()
274275

276+
def test_loop_factory_default_event_loop(self):
277+
async def main():
278+
if sys.platform == "win32":
279+
self.assertIsInstance(asyncio.get_running_loop(), asyncio.ProactorEventLoop)
280+
else:
281+
self.assertIsInstance(asyncio.get_running_loop(), asyncio.SelectorEventLoop)
282+
283+
284+
asyncio.run(main(), loop_factory=asyncio.EventLoop)
285+
275286

276287
class RunnerTests(BaseTest):
277288

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added :class:`asyncio.EventLoop` for use with the :func:`asyncio.run` *loop_factory* kwarg to avoid calling the asyncio policy system.

0 commit comments

Comments
 (0)