|
|
|
|
|
by scribu
209 days ago
|
|
> Awaiting a coroutine does not give control back to the event loop. I think this is a subtler point than one might think on first read, which is muddled due to the poorly chosen examples. Here's a better illustration: import asyncio
async def child():
print("child start")
await asyncio.sleep(0)
print("child end")
async def parent():
print("parent before")
await child() # <-- awaiting a coroutine (not a task)
print("parent after")
async def other():
for _ in range(5):
print("other")
await asyncio.sleep(0)
async def main():
other_task = asyncio.create_task(other())
parent_task = asyncio.create_task(parent())
await asyncio.gather(other_task, parent_task)
asyncio.run(main())
It prints: other
parent before
child start
other
child end
parent after
other
other
other
So the author's point is that "other" can never appear in-between "parent before" and "child start".Edit: clarification |
|