Closed
Description
asyncio's policy system deprecation
asyncio's policy system1 has been a source of confusion and problems in asyncio for a very long time. The policies no longer serve a real purpose. Loops are always per thread, there is no need to have a "current loop" when no loop is currently running. This issue discusses the changes to deprecate it in Python 3.14 and schedule its removal in 3.16 or later.
The usual user applications would use the runner APIs (see flowchart) while those who want more control like Jupyter project can create an event loop and manage it themselves, the difference would be that instead of them first getting the policy then event loop then can directly create it like
loop = MyCustomEventLoop()
loop.run_until_complete(task)
rather than currently
asyncio.set_event_loop_policy(MyPolicy())
policy = asyncio.get_event_loop_policy()
loop = policy.new_event_loop()
loop.run_until_complete(task)
See these discussions for more background:
- Deprecate get_event_loop() #83710 (comment)
- Finish deprecation in asyncio.get_event_loop() #93453 (comment)
- deprecate the asyncio child watchers system #94597 (comment)
- deprecate the asyncio child watchers system #94597
Functions and classes to be deprecated and later removed
-
asyncio.get_event_loop_policy
-
asyncio.set_event_loop_policy
-
asyncio.AbstractEventLoopPolicy
-
asyncio.DefaultEventLoopPolicy
-
asyncio.WindowsSelectorEventLoopPolicy
-
asyncio.WindowsProactorEventLoopPolicy
-
asyncio.set_event_loop
Functions to be modified
asyncio.get_event_loop
- In 3.16 or later this will become an alias toget_running_loop
.asyncio.new_event_loop
- In 3.16 or later this will ignore custom policies and will be an alias toasyncio.EventLoop
asyncio.run
&asyncio.Runner
- In 3.16 or later this will be modified to not use policy system as that will be gone and rely solely on loop_factory.
The Grand Plan
- To minimize changes, all the deprecated functions will be underscored i.e.
set_event_loop
->_set_event_loop
andset_event_loop
will emit the warning then call_set_event_loop
as its underlying implementation. This way internally asyncio can still call these functions until they are removed without need of many ignore warnings and the tests too can easily be adapted. - The deprecated classes will emit warnings when they are subclassed as it was done for child watchers.
- The runner APIs will be remain unmodified but making sure that correct warnings are emitted internally when policy system is used.
The Future
---
title: Flowchart for asyncio.run
---
flowchart TD
A["asyncio.run(coro, loop_factory=...)"] --> B{loop_factory}
B -->|loop_factory is None| D{platform}
B -->|loop_factory is not None| E["loop = loop_factory()"]
D --> |Unix| F["loop = SelectorEventLoop()"]
D --> |Windows| G["loop = ProactorEventLoop()"]
E --> H
F --> H
G --> H
H["loop.run_until_complete(coro)"]
Linked PRs
- gh-127949: deprecate
asyncio.set_event_loop_policy
#128024 - gh-127949: deprecate
asyncio.get_event_loop_policy
#128053 - gh-127949: fix resource warnings in test_tasks.py due to avoiding a call to get_event_loop() #128172
- gh-127949: fix DeprecationWarning in test_inspect.py #128215
- gh-127949: deprecate asyncio policy classes #128216
- gh-127949: deprecate
asyncio.set_event_loop
#128218 - gh-127949: add docs for asyncio policy deprecation #128269
- gh-127949: make deprecation of policy system more prominent #128290
- [3.13] gh-127949: fix resource warnings in
test_tasks.py
(GH-128172) #131805 - [3.12] gh-127949: fix resource warnings in
test_tasks.py
(GH-128172) #131806
Footnotes
Metadata
Metadata
Assignees
Labels
Projects
Status
Done