await

KeywordPython 3.5+Advanced

Pauses execution of an async coroutine until a result is available

Quick Info

Documentation
Official Docs
Python Version
3.5+

Learn by Difficulty

Quick Example

python
import asyncio

async def get_number():
    await asyncio.sleep(0)
    return 42

async def get_string():
    await asyncio.sleep(0)
    return "hello"

async def main():
    # await pauses until the coroutine completes
    num = await get_number()
    text = await get_string()
    print(f"Number: {num}")
    print(f"String: {text}")

asyncio.run(main())

await pauses the current coroutine until the awaited one completes, then returns its result. You can only use await inside async functions.

Try in Playground

Tags

languagesyntaxcoreasyncconcurrency

Related Items