π Async Programming (asyncio)¶
asyncio provides concurrency via cooperative multitasking:
- code runs in a single OS thread
- tasks voluntarily yield control at
await - great for "lots of I/O" workloads (many sockets, many requests)
Async is not automatically fasterβitβs a tool for scaling I/O concurrency while keeping code readable.
β
Coroutines and await¶
import asyncio
async def say_after(delay: float, msg: str) -> None:
await asyncio.sleep(delay)
print(msg)
asyncio.run(say_after(1.0, "hello"))
β
Tasks and gather()¶
asyncio.create_task(...)schedules a coroutine.asyncio.gather(...)waits for many tasks and returns results.
β Limiting Concurrency¶
Even in async code you sometimes need to limit parallel work (e.g. 1000 requests at once is still too many). Use an asyncio.Semaphore.
π Key Takeaways¶
async/awaitis for high-concurrency I/O.awaitis where other tasks can run.- Use tasks +
gather()to run many coroutines.