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
Open
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,92 @@ asyncio
* Removed implicit creation of event loop by :func:`asyncio.get_event_loop`.
It now raises a :exc:`RuntimeError` if there is no current event loop.
(Contributed by Kumar Aditya in :gh:`126353`.)
There's a few patterns that use :func:`asyncio.get_event_loop`, most
of them can be replaced with :func:`asyncio.run`.

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


async def main():
...

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?

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


async def main():
...

asyncio.run(main())

If you need to start something, e.g. a server listening on a socket
and then run forever, use :func:`asyncio.run` and an
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved
: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::


def start_server(loop):
...

loop = asyncio.get_event_loop()
try:
start_server(loop)
loop.run_forever()
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::


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


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


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

async def operation_one():
...

def blocking_code():
...

async def operation_two():
...

loop = asyncio.get_event_loop()
try:
loop.run_until_complete(operation_one())
blocking_code()
loop.run_until_complete(operation_two())
finally:
loop.close()

after::

async def operation_one():
...

def blocking_code():
...

async def operation_two():
...

with asyncio.Runner() as runner:
runner.run(operation_one())
blocking_code()
runner.run(operation_two())



collections.abc
Expand Down
Loading