Back
+30 XP
Concept

Fix the Event Loop Starvation: Offloading CPU Work

The asyncio event loop runs on a single thread. Executing heavy CPU computations like cryptographic hashing or image resizing directly inside a coroutine freezes the event loop, dropping heartbeats and stalling connections. Offload CPU work via loop.run_in_executor.

python
import asyncio

def heavy_cpu_work(data):
    # compute intensive logic
    return sum(i * i for i in data)

async def non_blocking_call(data):
    loop = asyncio.get_running_loop()
    return await loop.run_in_executor(None, heavy_cpu_work, data)

# Fix the Event Loop Starvation: Offloading CPU Work The golden rule of `asyncio` is simple: **never block the event loop**. If an incoming request executes a 300ms password hash or video frame resizing directly inside an `async def` function, every single other user on that server experiences a 300ms freeze. 🛑⚡ To keep microservices responsive, CPU-intensive algorithms must be delegated to background worker threads or processes via `loop.run_in_executor(None, sync_fn, *args)`. The event loop yields control, services pending network sockets, and resumes the coroutine when the thread returns. ```python import asyncio async def compute_async(cpu_bound_fn, *args): loop = asyncio.get_running_loop() # None uses the default ThreadPoolExecutor return await loop.run_in_executor(None, cpu_bound_fn, *args) ``` ## Pop Quiz What occurs when a synchronous blocking computation executes directly on the asyncio event loop thread? - [ ] The entire event loop halts; no timers fire and no pending I/O packets are processed until calculation ends. - [ ] The event loop automatically forks a worker subprocess to balance the CPU burden. - [ ] Python immediately raises a BlockingIOError and terminates the offending function. - [ ] The GIL redirects the task to background kernel threads via io_uring. ## Real-World Challenge Implement `offload_cpu_task(sync_fn, *args)`. Verify that running multiple CPU-bound operations concurrently through this helper completes without stalling asynchronous tasks running concurrently on the event loop. <details> <summary>Solution</summary> ```python import asyncio import hashlib def intensive_hashing(payload: str, iterations: int = 50000) -> str: data = payload.encode("utf-8") for _ in range(iterations): data = hashlib.sha256(data).digest() return data.hex() async def offload_cpu_task(sync_fn, *args): loop = asyncio.get_running_loop() return await loop.run_in_executor(None, sync_fn, *args) # Verification async def run_test(): heartbeat_ticks = 0 async def heartbeat(): nonlocal heartbeat_ticks for _ in range(5): heartbeat_ticks += 1 await asyncio.sleep(0.01) hb_task = asyncio.create_task(heartbeat()) cpu_task = asyncio.create_task(offload_cpu_task(intensive_hashing, "user_pass", 60000)) hash_result, _ = await asyncio.gather(cpu_task, hb_task) assert len(hash_result) == 64 assert heartbeat_ticks >= 3, f"Heartbeats starved! Only ran {heartbeat_ticks} times." asyncio.run(run_test()) ``` </details>