Advanced Async Programming
asyncio patterns, tasks, semaphores, and async generators.
Overview
Master advanced async patterns.
Tasks and Gather
import asyncio
async def fetch_data(url, delay):
print(f"Fetching {url}")
await asyncio.sleep(delay)
return f"Data from {url}"
async def main():
# Run tasks concurrently
tasks = [
fetch_data("api1.com", 2),
fetch_data("api2.com", 1),
fetch_data("api3.com", 3),
]
results = await asyncio.gather(*tasks)
print(results)
asyncio.run(main())
Semaphores
import asyncio
async def worker(semaphore, name, delay):
async with semaphore:
print(f"{name} started")
await asyncio.sleep(delay)
print(f"{name} finished")
async def main():
semaphore = asyncio.Semaphore(3) # Max 3 concurrent
tasks = [
worker(semaphore, f"Worker {i}", i)
for i in range(10)
]
await asyncio.gather(*tasks)
asyncio.run(main())
Async Generators
async def async_generator(n):
for i in range(n):
await asyncio.sleep(0.1)
yield i
async def main():
async for value in async_generator(5):
print(value)
asyncio.run(main())
Practice
Implement an async web scraper with rate limiting.