-
-
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?
Changes from 6 commits
e1b645c
9bb412d
4a8e47b
b98954d
588b20f
bdf970c
3405874
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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:: | ||||||||||
|
||||||||||
async def main(): | ||||||||||
... | ||||||||||
|
||||||||||
loop = asyncio.get_event_loop() | ||||||||||
try: | ||||||||||
loop.run_until_complete(amain()) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:: | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
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:: | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
def start_server(loop): | ||||||||||
... | ||||||||||
|
||||||||||
loop = asyncio.get_event_loop() | ||||||||||
try: | ||||||||||
start_server(loop) | ||||||||||
loop.run_forever() | ||||||||||
finally: | ||||||||||
loop.close() | ||||||||||
|
||||||||||
after:: | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
def start_server(loop): | ||||||||||
... | ||||||||||
|
||||||||||
async def amain(): | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why switch to
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs the :: for the code block to work |
||||||||||
|
||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
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 | ||||||||||
|
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.