Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Const

async

async is Python’s syntax family for coroutine functions and asynchronous control flow. It introduces async def, async for, and async with; await is used within coroutine bodies to suspend execution until an awaitable completes.

How it works

Python uses async syntax to define coroutine functions and asynchronous control-flow statements. Calling an async def function produces a coroutine; await suspends the current coroutine until an awaitable completes, while async for and async with provide asynchronous iteration and resource management inside coroutine functions.

Syntax

async def funcname([parameter_list]) [-> expression]: suite
async for_stmt
async with EXPRESSION as TARGET: SUITE

Key points

  • Use async def to define a coroutine function.
  • A coroutine can pause and later continue, allowing cooperative asynchronous execution.
  • async for consumes asynchronous iterables using asynchronous iteration.
  • An asynchronous iterable exposes __aiter__, which supplies its asynchronous iterator.
  • async with acquires and releases an asynchronous context-managed resource around a suite.

Common mistakes

  • Do not use await, async for, or async with outside a coroutine function; doing so is invalid.
  • async def always creates a coroutine function, even when its body contains no await or other async syntax.
  • An async with statement outside a coroutine function raises SyntaxError.

Example: Define and await a coroutine

import asyncio

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

async def main():
    value = await fetch_value()
    print(value)

asyncio.run(main())

Defines a coroutine with async def, suspends it with await, and runs it from a synchronous entry point.

When to use it

Coroutine functions

Define non-blocking operations that can pause while awaiting I/O or another awaitable.

Asynchronous iteration

Consume data from an asynchronous source as items become available.

Asynchronous context management

Manage resources whose enter and exit operations are asynchronous.

Interview questions

QWhat does async def do?

async def declares a coroutine function. Calling it returns a coroutine object, and the coroutine executes when it is awaited or otherwise scheduled by an event loop.

Follow-up: Can async def omit await? Yes. The documentation states that async def functions remain coroutine functions even without await or another async keyword.

QWhere may await be used?

await can appear only in a coroutine function body. It suspends the executing coroutine at an awaitable operation so other asynchronous work can proceed.

Follow-up: Where else is async syntax used? async for iterates asynchronous iterables, and async with manages asynchronous context managers.

QHow do async for and async with differ from their synchronous forms?

async for is the asynchronous counterpart of for: it works with an asynchronous iterable whose __aiter__ method supplies an asynchronous iterator. async with is the asynchronous counterpart of with and is restricted to coroutine bodies.

Follow-up: What does an asynchronous iterable provide? It provides __aiter__, which directly returns an asynchronous iterator capable of calling asynchronous code in __anext__.

  • await
  • async def
  • async for
  • async with
  • coroutine
  • asynchronous iterable
  • asynchronous iterator

Source: 8. Compound statements. Checked 2026-10-16.