Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
async and await SyntaxFormalized in PEP 492, the async and await keywords establish native asynchronous programming syntax in Python. By distinguishing coroutines from generator objects, native coroutines provide compile-time syntax validation, dedicated runtime protocols (__await__, __aiter__, __aenter__), and clear concurrency semantics.
await ExpressionDefining a function with async def creates a Native Coroutine Function. Calling this function does not execute its body; instead, it returns an unstarted Coroutine Object:
In Python, the await expression can only be used on objects that implement the Awaitable Protocol (defining an __await__() method). The three core awaitable types are:
async def.asyncio.create_task().async with)Just as synchronous code manages resources with with, asynchronous code uses async with to acquire and release network connections, transactions, and session locks non-blockingly.
An asynchronous context manager implements two dunder methods:
__aenter__(self): A coroutine that returns the acquired resource.__aexit__(self, exc_type, exc_val, exc_tb): A coroutine that performs cleanup.How data and code flow step-by-step
async for)Asynchronous iterators stream data non-blockingly (e.g. streaming chunks from an HTTP socket, WebSocket packets, or reading database query cursors).
They implement two methods:
__aiter__(self): Returns the asynchronous iterator object.__anext__(self): A coroutine that returns the next value, or raises StopAsyncIteration when exhausted.Just as standard generators use yield, an async def function containing yield creates an Asynchronous Generator. It allows you to produce data streams lazily while using await internally:
| Syntax | Underlying Protocol | Core Dunder Methods |
|---|---|---|
await expr | Awaitable Protocol | __await__() |
async with expr as val: | Async Context Manager | __aenter__(), __aexit__() |
async for item in expr: | Async Iterator | __aiter__(), __anext__() |
async def f(): yield x | Async Generator | __anext__(), asend(), aclose() |
await on a CoroutineCalling an async function without await returns a coroutine object without executing it:
await Outside of async defThe await keyword is syntactically invalid outside of functions declared with async def (except in Python 3.8+ top-level REPL shells).
Implement a custom asynchronous context manager using __aenter__ and __aexit__ to simulate non-blocking file streaming.
What occurs when an async def function is called directly without using the await keyword (e.g. result = my_coroutine())? A. The function executes immediately and returns its value. B. A coroutine object is created and returned, but its body is NOT executed yet, triggering a RuntimeWarning: coroutine was never awaited. C. A new OS thread is spawned. D. A SyntaxError is raised.
async def function returns a coroutine object. Its code does not begin execution until it is explicitly awaited or scheduled as an asyncio.Task on an event loop.Which three types of objects are considered valid "Awaitables" that can follow the await keyword in Python? A. Lists, Dictionaries, and Tuples B. Native Coroutines, Tasks, and Futures C. Threads, Processes, and Sockets D. Strings, Bytes, and Numbers
asyncio specification defines three primary awaitable objects: Coroutines (created by async def), Tasks (created via asyncio.create_task), and Futures (low-level callback trackers).Which dunder methods must a class implement to function as an asynchronous context manager with async with? A. __enter__ and __exit__ B. __aenter__ and __aexit__ C. __open__ and __close__ D. __start__ and __stop__
__aenter__(self) and __aexit__(self, exc_type, exc_val, exc_tb), both of which are coroutines evaluated with await.What exception must an asynchronous iterator raise from its __anext__() method to terminate an async for loop? A. StopIteration B. StopAsyncIteration C. GeneratorExit D. EOFError
StopIteration, asynchronous iterators must raise StopAsyncIteration to signal that no further items remain in the stream.Where is the await expression permitted to appear in Python code? A. Inside any standard Python function. B. Exclusively inside functions defined with async def (or within the interactive REPL in Python 3.8+). C. Only inside __init__ methods. D. Inside class definitions directly.
await outside of an async def function triggers a SyntaxError: 'await' outside async function.In the next lesson, we will continue your advanced Python journey with Creating and Managing Tasks (8: Asynchronous Programming (Asyncio)).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Introduction to Asyncio | Creating and Managing Tasks |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.