Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recommend what to use instead of asyncio.get_event_loop in 3.14 what's new #127174

Open
graingert opened this issue Nov 22, 2024 · 5 comments
Open
Labels
3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir topic-asyncio

Comments

@graingert
Copy link
Contributor

graingert commented Nov 22, 2024

          Could you please mention what should be used instead?

Originally posted by @hroncok in #126354 (comment)

Linked PRs

@graingert
Copy link
Contributor Author

graingert commented Nov 22, 2024

Currently:

import asyncio

async def amain():
    await asyncio.sleep(0)
    print("slept")

asyncio.get_event_loop().run_until_complete(amain())

Will fail on 3.14

Code should use

import asyncio

async def amain():
    await asyncio.sleep(0)
    print("slept")

asyncio.run(amain())

Instead

It may be that a user will need to interleave synchronous code with calls into and back out of the event loop in which case with asyncio.Runner() as runner: runner.run(...) should be used

It is not correct to simply replace asyncio.get_event_loop() with asyncio.new_event_loop() because there's a number of important teardown steps that asyncio.Runner performs. Making this change will appear to work, but will silently cause problems

@graingert graingert changed the title recommend what to use instead of asyncio.get_event_loop recommend what to use instead of asyncio.get_event_loop in 3.14 what's new Nov 22, 2024
@graingert graingert added topic-asyncio stdlib Python modules in the Lib dir 3.14 new features, bugs and security fixes labels Nov 22, 2024
@github-project-automation github-project-automation bot moved this to Todo in asyncio Nov 22, 2024
@asvetlov
Copy link
Contributor

asvetlov commented Dec 3, 2024

Sorry, I've missed. What part of docs should be corrected?

@graingert
Copy link
Contributor Author

It's the what's new section

@kumaraditya303
Copy link
Contributor

@graingert Would you like to send a PR for it? I'll review it.

@graingert
Copy link
Contributor Author

I'll give it a go, feel free for someone else to do it if it's not done by next week

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir topic-asyncio
Projects
Status: Todo
Development

No branches or pull requests

3 participants