Concurrency Throttling with asyncio.Semaphore
Spawning 10,000 unthrottled coroutines causes file descriptor exhaustion (`EMFILE`) and upstream rate-limit bans. `asyncio.Semaphore(N)` guards shared critical sections, limiting active concurrency to $N$ coroutines while queuing excess callers 🚦🛡️.
import asyncio
async def limited_fetch(sem, task_id):
async with sem:
# Only N tasks enter this block concurrently:
print(f"Task {task_id} acquired lease 🚦")
await asyncio.sleep(0.02)
print(f"Task {task_id} released lease 🏁")
async def main():
sem = asyncio.Semaphore(2) # Max 2 concurrent tasks
await asyncio.gather(*(limited_fetch(sem, i) for i in range(4)))
asyncio.run(main())
# 20. Concurrency Throttling with asyncio.Semaphore "Async is fast, so let's fire 50,000 HTTP requests simultaneously!" is the fastest way to get your IP blacklisted or crash your Linux process with `OSError: [Errno 24] Too many open files` 🛑. ```python import asyncio async def query(sem, endpoint): async with sem: # Protected section: return await make_network_call(endpoint) ``` ### Concurrency Control Architecture 1. **The Counter**: An internal atomic counter initializes to $N$. Each `acquire()` decrements it. When the counter reaches 0, callers suspend on an internal `collections.deque` of futures. 2. **Context Manager Pattern**: Always use `async with sem:`. If an exception occurs inside the block, the context manager's `__aexit__` guarantees the counter increments so other queued tasks do not deadlock 🛡️. 3. **BoundedSemaphore Alternative**: If you need strict protection against accidental extra `.release()` calls, use `asyncio.BoundedSemaphore`, which raises `ValueError` if released more times than acquired. --- ### Interactive Pop-Quiz 🧠 #### What happens when a coroutine calls `await sem.acquire()` when all semaphore slots are in use? - [ ] The coroutine frame suspends execution until another task exits the semaphore context and releases a slot 🚦 - [ ] It raises a `ConcurrencyLimitExceeded` runtime exception - [ ] It executes synchronously on a worker thread pool instead - [ ] It drops the request and sets the return value to None --- ### Practical Challenge 🎯 Create a semaphore with a limit of 2. Spawn 5 tasks that enter the semaphore, record their `task_id` into a list `active`, assert `len(active) <= 2`, simulate work with `await asyncio.sleep(0.01)`, and remove their `task_id` before exiting. Print `'THROTTLING_OK'` when all tasks complete. <details> <summary>Solution</summary> ```python import asyncio async def worker(sem, task_id, active): async with sem: active.append(task_id) assert len(active) <= 2 await asyncio.sleep(0.01) active.remove(task_id) async def main(): sem = asyncio.Semaphore(2) active = [] await asyncio.gather(*(worker(sem, i, active) for i in range(5))) print("THROTTLING_OK") asyncio.run(main()) ``` </details>