Python Async
Asynchronous Programming with asyncio
Why Async?
Asynchronous programming allows Python to handle multiple I/O-bound tasks concurrently without blocking. Instead of waiting idle
for a network response or file read, the program switches to other tasks. Ideal for web servers, APIs, and any I/O-heavy workloads.
Sync vs Async
# SYNC - blocks for 2 seconds total
def fetch_sync():
[Link](1) # wait
[Link](1) # wait again
# Total: 2 seconds
# ASYNC - runs concurrently
async def fetch_async():
await [Link](
[Link](1),
[Link](1),
)
# Total: ~1 second
Core Concepts
async def my_coroutine():
# async def → defines a coroutine
# await → suspends until result ready
# gather() → run multiple coroutines
result = await some_io_operation()
return result
# Run the coroutine
[Link](my_coroutine())
[Link]
async def task(name, delay):
await [Link](delay)
return f'{name} done'
async def main():
results = await [Link](
task('A', 1),
task('B', 2),
task('C', 1),
)
# All run concurrently
# Total time: ~2s not 4s
print(results)
async with httpx
import httpx
async def fetch_url(url: str):
async with [Link]() as client:
response = await [Link](url)
return [Link]()
async def main():
urls = ['[Link]
'[Link]
results = await [Link](
*[fetch_url(u) for u in urls]
)
Python Reference Sheet — Python Async | Generated 2026