GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-93453: No longer create an event loop in get_event_loop() by serhiy-storchaka · Pull Request #98440 · python/cpython

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Oct 19, 2022 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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.

  • Issue: Finish deprecation in asyncio.get_event_loop() #93453

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@serhiy-storchaka
pythongh-93453: No longer create an event loop in get_event_loop()
e789d32
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.
@bedevere-bot bedevere-bot added the awaiting core review label Oct 19, 2022
@serhiy-storchaka serhiy-storchaka mentioned this pull request Oct 19, 2022
Finish deprecation in asyncio.get_event_loop() #93453
Closed
@blink1073

blink1073 commented Oct 19, 2022

Copy link
Copy Markdown

👍🏼 to this approach. It would allow us to use the following pattern in jupyter_client to be able to create a ready future in a constructor when there is no event loop running:

def __init__(self):
      try:
            self._ready = asyncio.Future()
      except RuntimeError:
            asyncio.set_event_loop(get_event_loop_policy().get_event_loop())
            self._ready = asyncio.Future()

Similarly, in motor (an asyncio version of pymongo), we currently delay the creation of the event loop used by the async client, and could do so in the constructor as well.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gvanrossum
gvanrossum approved these changes Oct 22, 2022
View reviewed changes

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a doc nit -- this looks very thorough and I think we should go with this. However, I would like to double check with @1st1. (I also wish @asvetlov could have a look but I don't want to put pressur on him at all.) So let's not merge this yet until we hear from at least one of them.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/library/asyncio-eventloop.rst
Comment on lines -58 to -65
.. deprecated:: 3.10
Deprecation warning is emitted if there is no running event loop.
In future Python releases, this function will be an alias of
:func:`get_running_loop`.

@gvanrossum gvanrossum Oct 22, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should add a note telling user that in 3.10 and 3.11 (at least up to a certain patch level) this situation would emit a warning.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting core review labels Oct 22, 2022
@kumaraditya303
kumaraditya303 self-requested a review October 31, 2022 17:04
@kumaraditya303 kumaraditya303 added the topic-asyncio label Oct 31, 2022
@blink1073 blink1073 mentioned this pull request Nov 2, 2022
Comms with async functions jupyter/jupyter_client#868
Open
@gvanrossum

gvanrossum commented Nov 29, 2022

Copy link
Copy Markdown
Member

@serhiy-storchaka Do you want to undraft this? Then we can do a final review and merge it.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka
serhiy-storchaka marked this pull request as ready for review November 30, 2022 04:53
@serhiy-storchaka
serhiy-storchaka requested review from 1st1 and asvetlov as code owners November 30, 2022 04:53
serhiy-storchaka added 2 commits November 30, 2022 12:50
@serhiy-storchaka
Merge branch 'main' into asyncio-get_event_loop2
4a6e405
@serhiy-storchaka
Fix test_fork_not_share_event_loop.
380c158
gvanrossum
gvanrossum approved these changes Nov 30, 2022
View reviewed changes

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great -- you can merge now! I won't insist on waiting for Yury.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@serhiy-storchaka

serhiy-storchaka commented Dec 1, 2022

Copy link
Copy Markdown
Member Author

I do not know the original purpose of the deprecation, so I am not sure that it is the right way.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

kumaraditya303
kumaraditya303 reviewed Dec 1, 2022
View reviewed changes

@kumaraditya303 kumaraditya303 left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but please add a what's new entry for this.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@serhiy-storchaka

serhiy-storchaka commented Dec 1, 2022

Copy link
Copy Markdown
Member Author

Ah, yes, it was a reason why this PR was a draft. Thank you for a reminder.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka
serhiy-storchaka marked this pull request as draft December 1, 2022 12:58
@gvanrossum

gvanrossum commented Dec 1, 2022

Copy link
Copy Markdown
Member

I do not know the original purpose of the deprecation, so I am not sure that it is the right way.

Users were getting confused by the meaning of get_event_loop(), which sometimes created a new loop and sometimes didn't. They also misunderstood the meaning of set_event_loop() and would create and set a new loop when a loop already existed. We were so frustrated with this that we decided to change the semantics of get_event_loop() to only return the currently-running loop (i.e., get_running_loop()), but since that was a semantic change we introduced a deprecation first.

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 get_event_loop() should never create a new loop (it's better to have control over that and use new_event_loop() or better asyncio.run()) but it should still be possible to set a loop in the policy object's thread locals, and get that.

The PR implements that, AFAICT.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Dec 1, 2022

Copy link
Copy Markdown
Member Author

Thank you. I know about problems with creating a new loop in get_event_loop(). Initially Andrew was going to deprecate get_event_loop() at all, but I proposed to keep in case if it returns an existing loop, so the existing code written before introduction of get_running_loop() will not need changes. But a corner case, when get_event_loop() returns an existing loop which is not a running loop was not clear to me.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka
Update docs.
a09eccb
@serhiy-storchaka

serhiy-storchaka commented Dec 1, 2022

Copy link
Copy Markdown
Member Author

I am planning to merge this PR only after creating a PR for 3.11 to undeprecate what was incorrectly deprecated.

gvanrossum reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gvanrossum
gvanrossum approved these changes Dec 1, 2022
View reviewed changes
@serhiy-storchaka

serhiy-storchaka commented Dec 2, 2022

Copy link
Copy Markdown
Member Author

See #99949.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka
serhiy-storchaka marked this pull request as ready for review December 2, 2022 11:12
kumaraditya303
kumaraditya303 approved these changes Dec 2, 2022
View reviewed changes
@ambv
Merge branch 'main' into asyncio-get_event_loop2
72223eb
@ambv
Address wording nits
0b4cbfe
@ambv ambv mentioned this pull request Dec 5, 2022
[3.11] gh-93453: Only emit deprecation warning in asyncio.get_event_loop when a new event loop is created #99949
Merged
@ambv

ambv commented Dec 5, 2022

Copy link
Copy Markdown
Contributor

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gvanrossum
gvanrossum approved these changes Dec 6, 2022
View reviewed changes

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still LGTM. But yeah, let's wait until the 3.11/3.10 PRs have landed.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ambv reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@ambv
ambv merged commit fd38a2f into python:main Dec 6, 2022
@bedevere-bot bedevere-bot removed the awaiting merge label Dec 6, 2022
@amotl amotl mentioned this pull request Dec 12, 2022
CI croaks on Python 3.12.0a3 grafana-toolbox/grafana-wtf#46
Closed
@serhiy-storchaka
serhiy-storchaka deleted the asyncio-get_event_loop2 branch December 21, 2022 15:04
@pepoluan pepoluan mentioned this pull request Dec 29, 2022
Workaround for asyncio.get_event_loop() deprecation aio-libs/aiosmtpd#355
Merged
5 tasks done
@bdarnell bdarnell mentioned this pull request Jan 17, 2023
Reverse some deprecation warnings from Tornado 6.2 tornadoweb/tornado#3216
Closed
@bdarnell bdarnell mentioned this pull request Feb 19, 2025
asyncio: Re-reverse deprecation of set_event_loop? #130322
Closed
@coderabbitai coderabbitai Bot mentioned this pull request Apr 14, 2026
fix(ingestion): prevent nba_api timeout bursts and persist failed player ingests tomiwaaluko/chalk#8
Merged
@douglasdotc douglasdotc mentioned this pull request May 27, 2026
[BugFix] Replace deprecated asyncio.get_event_loop() in check_connection canonical/yarf#256
Merged
p-gentili pushed a commit to canonical/yarf that referenced this pull request Jun 1, 2026
@douglasdotc
[BugFix] Replace deprecated asyncio.get_event_loop() in check_conne…
f2931c2
…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.
@coderabbitai coderabbitai Bot mentioned this pull request Jun 3, 2026
Python 3.14 support - Fix asyncio initialization TeskaLabs/asab#763
Merged
@coderabbitai coderabbitai Bot mentioned this pull request Jun 19, 2026
feat(agent): add module-level debug logging for canvas execution flow infiniflow/ragflow#16200
Merged
This was referenced Aug 15, 2026
The desktop app: Lab in its own renderer, app state out of the workspace, and one instance in the tray view321/Grad#6
Merged
feat(capsule): node capsule launcher — install custom nodes at runtime (PoC) rocketride-org/rocketride-server#1782
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@gvanrossum gvanrossum gvanrossum approved these changes
@kumaraditya303 kumaraditya303 kumaraditya303 approved these changes
@1st1 1st1 Awaiting requested review from 1st1 1st1 is a code owner
@asvetlov asvetlov Awaiting requested review from asvetlov asvetlov is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

topic-asyncio

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@serhiy-storchaka @blink1073 @gvanrossum @ambv @kumaraditya303 @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.