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

Merged
merged 9 commits into from
Dec 18, 2024
90 changes: 90 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,96 @@ asyncio
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::

async def main():
...


loop = asyncio.get_event_loop()
try:
loop.run_until_complete(main())
finally:
loop.close()

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

def start_server(loop):
...

loop = asyncio.get_event_loop()
try:
start_server(loop)
loop.run_forever()
finally:
loop.close()

After::

def start_server(loop):
...

async def main():
start_server(asyncio.get_running_loop())
await asyncio.Event().wait()

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

kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved
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