Polish doc as part of asyncio doc improvement#9185
Conversation
Signed-off-by: Carol Willing <[email protected]>
There was a problem hiding this comment.
ensure_future() is something that we need to deprioritize in the docs -- it's low-level and very confusing. asyncio.create_task() is the preferred way to spawn asyncio tasks.
Same for loop.create_task() -- it's a low-level advanced API that few need to ever use directly.
ee9deed to
fb64f8c
Compare
| loop.close() | ||
|
|
||
| Another option is to use the :meth:`loop.run_until_complete` | ||
| (or alternatively, the provisional :meth:`asyncio.run`) |
There was a problem hiding this comment.
:func:`asyncio.run`, not :meth:
Nit picking: I wouldn't focus too much on the fact that asyncio.run is provisional -- the only reason for it to be provisional is because we're unsure about some obscure edge cases. The function won't be removed and it's the preferred way of running an asyncio program (as it's almost impossible to use loop.run_until_complete API correctly :( ).
There was a problem hiding this comment.
Nit picking makes clear docs ;-) Will update now.
| task = asyncio.ensure_future(bug()) | ||
| try: | ||
| loop.run_until_complete(task) | ||
| asyncio.run(task) |
There was a problem hiding this comment.
Just asyncio.run(bug()), the asyncio.ensure_future(bug()) is unnecessary.
| loop.run_until_complete(task) | ||
| except Exception: | ||
| print("exception consumed") | ||
| asyncio.run(bug()) |
There was a problem hiding this comment.
I not sure but maybe keeping try/except block around asyncio.run(bug()) makes sense?
It teaches how to catch asyncio exceptions on very high level
There was a problem hiding this comment.
Well, catching exceptions around of asyncio.run() is kind of outside of the highest level of asyncio :)
asyncio.create_taskandasyncio.run