-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
gh-127174: add some advice for asyncio.get_event_loop replacements #127640
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
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 think a few small changes would be good.
Doc/whatsnew/3.14.rst
Outdated
def start_server(loop): | ||
... | ||
|
||
async def amain(): |
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.
Why switch to amain()
instead of main()
like before? I think all examples should use a consistent style.
async def amain(): | |
async def main(): |
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.
Yeah I prefer the amain, it's compatible with idiomatic usage of console scripts:
import asyncio
import sys
async def amain():
...
def main():
return asyncio.run(amain())
if __name__ == "__main__":
sys.exit(main())
Doc/whatsnew/3.14.rst
Outdated
start_server(asyncio.get_running_loop()) | ||
await asyncio.Event().wait() | ||
|
||
asyncio.run(amain()) |
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.
asyncio.run(amain()) | |
asyncio.run(main()) |
Doc/whatsnew/3.14.rst
Outdated
asyncio.run(amain()) | ||
|
||
If you need to run something in an event loop, then run some blocking | ||
code around it, use :class:`asyncio.Runner`, before:: |
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.
code around it, use :class:`asyncio.Runner`, before:: | |
code around it, use :class:`asyncio.Runner`. |
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.
This needs the :: for the code block to work
|
||
If you need to run something in an event loop, then run some blocking | ||
code around it, use :class:`asyncio.Runner`, before:: | ||
|
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.
before:: | |
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.
Your before/after should be capitalized as they introduce a new paragraph. To reduce the length of the text, how about putting ...
on the same line as def
?
|
||
loop = asyncio.get_event_loop() | ||
try: | ||
loop.run_until_complete(amain()) |
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.
Can you use 4-spaces indents in the Python code please?
|
||
If you're running an async function, simply use :func:`asyncio.run`. | ||
|
||
before:: |
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.
before:: | |
Before:: |
finally: | ||
loop.close() | ||
|
||
after:: |
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.
after:: | |
After:: |
and then run forever, use :func:`asyncio.run` and an | ||
:class:`asyncio.Event`. | ||
|
||
before:: |
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.
before:: | |
Before:: |
finally: | ||
loop.close() | ||
|
||
after:: |
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.
after:: | |
After:: |
📚 Documentation preview 📚: https://cpython-previews--127640.org.readthedocs.build/