- Event loop quản lý coroutines.
- Khi coroutine gặp
await, nó tạm dừng và nhường control để event loop chạy coroutine khác. - Đây là cooperative concurrency — không phải parallelism thực sự.
python
import asyncio, httpx
async def fetch(url: str) -> dict:
async with httpx.AsyncClient() as client:
r = await client.get(url) # Yield control tại đây
return r.json()
async def main():
# Chạy 3 requests đồng thời (concurrent), không phải song song
results = await asyncio.gather(
fetch("/users/1"),
fetch("/users/2"),
fetch("/users/3"),
)
asyncio.run(main())