gh-93453: No longer create an event loop in get_event_loop()#98440
Conversation
asyncio.get_event_loop() now always return either running event loop or the result of get_event_loop_policy().get_event_loop() call. The latter should now raise an RuntimeError if no current event loop was set instead of creating and setting a new event loop. It affects also a number of asyncio functions and constructors which call get_event_loop() implicitly: ensure_future(), shield(), gather(), etc. DeprecationWarning is no longer emitted if there is no running event loop but the current event loop was set.
|
👍🏼 to this approach. It would allow us to use the following pattern in Similarly, in motor (an |
Sorry, something went wrong.
gvanrossum
left a comment
There was a problem hiding this comment.
Sorry, something went wrong.
|
@serhiy-storchaka Do you want to undraft this? Then we can do a final review and merge it. |
Sorry, something went wrong.
gvanrossum
left a comment
There was a problem hiding this comment.
Looks great -- you can merge now! I won't insist on waiting for Yury.
Sorry, something went wrong.
|
I do not know the original purpose of the deprecation, so I am not sure that it is the right way. |
Sorry, something went wrong.
kumaraditya303
left a comment
There was a problem hiding this comment.
LGTM, but please add a what's new entry for this.
Sorry, something went wrong.
|
Ah, yes, it was a reason why this PR was a draft. Thank you for a reminder. |
Sorry, something went wrong.
Users were getting confused by the meaning of Then we heard from users who did understand the mechanism and were using it to solve an issue they couldn't easily solve in any other way, and we changed our mind. The current idea is that The PR implements that, AFAICT. |
Sorry, something went wrong.
|
Thank you. I know about problems with creating a new loop in |
Sorry, something went wrong.
|
I am planning to merge this PR only after creating a PR for 3.11 to undeprecate what was incorrectly deprecated. |
Sorry, something went wrong.
|
NOTE: this PR assumes the fix backports will be landed in time for 3.10.9 and 3.11.1. This is currently in question (see GH-99949) so please monitor the 3.10 and 3.11 PRs before landing this one. |
Sorry, something went wrong.
gvanrossum
left a comment
There was a problem hiding this comment.
Still LGTM. But yeah, let's wait until the 3.11/3.10 PRs have landed.
Sorry, something went wrong.
…ction (#256) The sync check_connection entrypoints in the Mir and Vnc platforms called asyncio.get_event_loop() to obtain (or implicitly create) an event loop. Since CPython PR python/cpython#98440 (Python 3.12), that call no longer auto-creates a loop once anything in the process has touched the loop policy and raises RuntimeError instead. With the recent pytest-asyncio 1.x and pytest 9 bumps, xdist workers no longer incidentally have a loop pre-installed.
asyncio.get_event_loop() now always return either running event loop or the result of get_event_loop_policy().get_event_loop() call. The latter should now raise an RuntimeError if no current event loop was set instead of creating and setting a new event loop.
It affects also a number of asyncio functions and constructors which call get_event_loop() implicitly: ensure_future(), shield(), gather(), etc.
DeprecationWarning is no longer emitted if there is no running event loop but the current event loop was set.