Skip to content

Event loop

Category: Scheduling · Status: stub · Lessons: chapter 06, Async (planned)

One line: A loop on one thread that waits for events — a socket ready, a timer due, work finished — and runs the callbacks or resumes the tasks waiting on each.

Also called: run loop, message loop.

How it connects

flowchart LR
  n_async_runtime["Async runtime (executor and reactor)"]
  n_event_loop["Event loop"]
  n_event_driven_programming["Event-driven programming"]
  n_io_multiplexing["I/O multiplexing"]
  n_async_runtime -->|uses| n_event_loop
  n_event_driven_programming -->|uses| n_event_loop
  n_event_loop -->|uses| n_io_multiplexing
  classDef center stroke-width:3px
  class n_event_loop center
  classDef outside stroke-dasharray: 4 3
  class n_async_runtime,n_event_driven_programming,n_io_multiplexing outside

In each language

Rust None in the standard library; an async runtime such as Tokio ↗ provides it
C Written by hand around poll, or taken from an event library
Python The asyncio event loop ↗; applications normally start it with asyncio.run and rarely touch the loop object
JavaScript Always there, provided by the host; microtasks ↗ such as promise callbacks run before control returns to the event loop
Swift RunLoop in Foundation
Erlang and Elixir A GenServer is a process whose receive loop the behaviour writes for you

Where to read more