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

gh-127174: add some advice for asyncio.get_event_loop replacements #127640

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

graingert
Copy link
Contributor

@graingert graingert commented Dec 5, 2024

@bedevere-app bedevere-app bot added docs Documentation in the Doc dir skip news labels Dec 5, 2024
@graingert graingert marked this pull request as ready for review December 5, 2024 15:54
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Copy link
Contributor

@StanFromIreland StanFromIreland left a 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.

def start_server(loop):
...

async def amain():
Copy link
Contributor

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.

Suggested change
async def amain():
async def main():

Copy link
Contributor Author

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())

start_server(asyncio.get_running_loop())
await asyncio.Event().wait()

asyncio.run(amain())
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
asyncio.run(amain())
asyncio.run(main())

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::
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
code around it, use :class:`asyncio.Runner`, before::
code around it, use :class:`asyncio.Runner`.

Copy link
Contributor Author

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::

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
before::

Doc/whatsnew/3.14.rst Outdated Show resolved Hide resolved
Doc/whatsnew/3.14.rst Outdated Show resolved Hide resolved
Doc/whatsnew/3.14.rst Outdated Show resolved Hide resolved
Copy link
Contributor

@picnixz picnixz left a 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())
Copy link
Contributor

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::
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
before::
Before::

finally:
loop.close()

after::
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
after::
After::

and then run forever, use :func:`asyncio.run` and an
:class:`asyncio.Event`.

before::
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
before::
Before::

finally:
loop.close()

after::
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
after::
After::

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review docs Documentation in the Doc dir skip news
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

4 participants