Python await Keyword
Example
Use await
to wait for a coroutine result inside async
code:
import asyncio
async def greet():
return "Hi"
async def main():
msg = await greet()
print(msg)
asyncio.run(main())
Try it Yourself »
Definition and Usage
The await
keyword pauses execution in an async
function until the awaited object (coroutine / awaitable) returns a result.
await
can only be used inside functions declared with async
.
More Examples
Example
Await multiple tasks with asyncio.gather()
:
import asyncio
async def one():
return "one"
async def two():
return "two"
async def main():
a, b = await asyncio.gather(one(), two())
print(a, b)
asyncio.run(main())
Try it Yourself »
Related Pages
The async
keyword.