import dis
import asyncio
asyncdefsimple_await():
await asyncio.sleep(0)
return1asyncdefmulti_await():
a = await asyncio.sleep(0)
b = await asyncio.sleep(0)
return a, b
print("Single await:")
dis.dis(simple_await)
print("\nMultiple awaits:")
dis.dis(multi_await)
# await compiles to:# 1. Evaluate the awaitable expression# 2. GET_AWAITABLE - calls __await__() to get iterator# 3. LOAD_CONST None# 4. SEND / YIELD_VALUE - yield control to event loop# The event loop drives the coroutine via send()
Output
Click "Run" to execute your code
await compiles to GET_AWAITABLE (which calls __await__) followed by SEND. The event loop drives coroutines by repeatedly calling send(None) until StopIteration, which carries the return value.
Challenge
Try modifying the code above to explore different behaviors. Can you extend the example to handle a new use case?