Asynchronous I/O
Pause an async operation until its result is available without blocking the surrounding event loop.
Python · Const
await suspends a coroutine until an awaitable completes, then yields that awaitable’s result. It is valid only inside a coroutine function.
Inside a coroutine function, await pauses that coroutine until an awaitable completes and produces a result. The operand follows the primary-expression form, so await can be applied to coroutine objects and other awaitables such as tasks or futures.
await primary| Name | Type | Description |
|---|---|---|
primary | awaitable expression | The primary expression producing the awaitable to suspend on. |
The result produced by the awaited object after the coroutine resumes.
import asyncio
async def get_value():
await asyncio.sleep(0)
return 42
async def main():
value = await get_value()
print(value)
asyncio.run(main())The coroutine pauses at await until get_value finishes, then continues with its returned value.
Pause an async operation until its result is available without blocking the surrounding event loop.
Sequence dependent coroutine operations while preserving readable control flow.
Consume results from asynchronous generators or their methods.
await suspends the current coroutine on an awaitable and resumes it when that awaitable completes. The expression evaluates to the value produced by the awaitable.
Follow-up: What is the grammar for await? It is await followed by a primary expression: await primary.
await can appear only inside a coroutine function, normally one declared with async def. It is not a general-purpose blocking operator for synchronous functions.
Follow-up: How can synchronous code enter an async workflow? Run the top-level coroutine with an event-loop entry point such as asyncio.run.
An awaitable is an object usable in an await expression. Common awaitables include coroutine objects, tasks, and futures.
Follow-up: What determines whether a custom object is awaitable? Its await protocol, exposed through __await__, determines how awaiting proceeds.
async defasync forasynciocoroutine functionawaitableTaskFuture__await__