What does await actually do inside an async function?hard

Type
conceptual
Topic
async Python
Frequency
common
Tags
async, await, event loop, coroutines
Answer

await suspends the current coroutine and returns control to the event loop, which runs other ready coroutines until the awaited operation completes.

Explanation

Calling an async def function without await returns a coroutine object — nothing runs. await actually executes it. When await is hit, the coroutine yields control to the event loop. The event loop runs the next ready task and resumes the suspended coroutine when the IO completes. This cooperative multitasking lets one thread handle thousands of concurrent IO operations.

Follow-upWhat's the difference between asyncio.gather and asyncio.create_task?