Keywords and Control Flow

async — Asynchronous definitions and suites

Mark a function as a native coroutine function, or as an asynchronous generator function if its own body contains yield. Calling a coroutine function creates a coroutine object without running its body immediately; execution requires awaiting or scheduling it. async for consumes asynchronous iterators, and async with uses asynchronous context-manager methods. These forms do not automatically run code in another thread or make blocking operations nonblocking. Ordinary scripts require async for and async with statements inside async functions.

Formal Syntax & Structure

python syntaxpython
async def name(parameters):    suite
async for target in iterable:    suite
async with manager as target:    suite

Working Example

Example Snippetpython
import asyncio
async def readings():    for number in (3, 5):        yield number
async def main():    values = []    async for number in readings():        values.append(number * 2)    print(values)
asyncio.run(main())
Python Reference