Writing a Barebones Async Event Loop from Scratch
An event loop is not magic. At its core, it is a single-threaded `while` loop that pops ready callbacks from a double-ended queue (`deque`), steps suspended coroutine frames with `.send(None)`, and schedules them again until exhausted 🔁⚙️.
from collections import deque
class BarebonesLoop:
def __init__(self):
self.ready = deque()
def call_soon(self, fn, *args):
self.ready.append((fn, args))
def step_coro(self, coro):
def step():
try:
coro.send(None)
self.call_soon(step)
except StopIteration:
pass
self.call_soon(step)
def run_forever(self):
while self.ready:
fn, args = self.ready.popleft()
fn(*args)
def worker():
print("Step 1 🚀")
yield
print("Step 2 🏁")
loop = BarebonesLoop()
loop.step_coro(worker())
loop.run_forever()
# 18. Writing a Barebones Async Event Loop from Scratch Frameworks like `asyncio`, `Trio`, and Node.js hide the mechanics, leading devs to think asynchronous execution is multi-threaded witchcraft. In reality, it is a simple FIFO loop stepping through generator frames 🛑. ```python from collections import deque queue = deque() def schedule(coro): def step(): try: coro.send(None) queue.append(step) # Re-queue if not finished except StopIteration: pass # Coroutine completed queue.append(step) ``` ### Cooperative Multitasking Pipeline 1. **Voluntary Yielding**: Unlike OS thread preemption where the kernel forcibly pauses execution, coroutines explicitly hand control back via `yield` or `await` 🔄. 2. **Task Interleaving**: When Coroutine A pauses, the loop pulls Coroutine B from `deque`, advancing it until it pauses, creating interleaved concurrency on a single OS thread. 3. **Completion Trapping**: When a generator finishes, CPython raises `StopIteration`. The event loop catches this signal and drops the task from its scheduler queue 🎯. --- ### Interactive Pop-Quiz 🧠 #### How does a cooperative event loop achieve multitasking without multiple OS threads? - [ ] Coroutines voluntarily yield control back to the loop queue at designated suspension points 🔄 - [ ] The CPU hardware interrupts the Python process every 5 milliseconds to switch threads - [ ] The Linux kernel forks a separate process for every coroutine execution frame - [ ] The loop uses preemptive bytecode rewriting to hijack local variable scopes --- ### Practical Challenge 🎯 Implement `MiniLoop` with `call_soon(fn, *args)`, `spawn(coro)`, and `run()`. Define two generator tasks `t1` and `t2` that append `'A1'`, `'A2'`, `'B1'`, `'B2'` across yields. Run both tasks and print the recorded execution trace. <details> <summary>Solution</summary> ```python from collections import deque class MiniLoop: def __init__(self): self.ready = deque() def call_soon(self, fn, *args): self.ready.append((fn, args)) def spawn(self, coro): def advance(): try: coro.send(None) self.call_soon(advance) except StopIteration: pass self.call_soon(advance) def run(self): while self.ready: fn, args = self.ready.popleft() fn(*args) trace = [] def task_a(): trace.append('A1') yield trace.append('A2') def task_b(): trace.append('B1') yield trace.append('B2') loop = MiniLoop() loop.spawn(task_a()) loop.spawn(task_b()) loop.run() print(trace) ``` </details>