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

async def amain():
...

loop = asyncio.get_event_loop()
try:
loop.run_until_complete(amain())
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved
finally:
loop.close()

after::
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved

async def amain():
...

asyncio.run(amain())
graingert marked this conversation as resolved.
Show resolved Hide resolved

If you need to start something, eg, a server listening on a socket
graingert marked this conversation as resolved.
Show resolved Hide resolved
and then run forever, use :func:`asyncio.run` and an
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved
:class:`asyncio.Event`, before::
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved

def start_server(loop):
...

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

after::
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved

def start_server(loop):
...

async def amain():
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved
start_server(asyncio.get_running_loop())
await asyncio.Event().wait()

asyncio.run(amain())
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved

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

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

If you need an 'ambient' event loop, shared between multiple otherwise
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved
graingert marked this conversation as resolved.
Show resolved Hide resolved
unrelated modules, this is the sort of use-case that's no longer supported
graingert marked this conversation as resolved.
Show resolved Hide resolved
in asyncio.
graingert marked this conversation as resolved.
Show resolved Hide resolved


collections.abc
Expand Down
Loading