Skip to content

Event-driven programming

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

One line: A program built as handlers that run when events arrive — a click, a message, a ready socket — instead of as one flow from top to bottom.

Also called: event-driven architecture.

How it connects

flowchart LR
  n_callback["Callback"]
  n_event_loop["Event loop"]
  n_event_driven_programming["Event-driven programming"]
  n_reactive_programming["Reactive programming"]
  n_event_driven_programming ---|vs| n_reactive_programming
  n_event_driven_programming -->|uses| n_callback
  n_event_driven_programming -->|uses| n_event_loop
  classDef center stroke-width:3px
  class n_event_driven_programming center
  classDef outside stroke-dasharray: 4 3
  class n_callback,n_event_loop,n_reactive_programming outside

In each language

Go no event loop in user code: a goroutine makes an ordinary blocking call, and the runtime moves the other goroutines to a runnable thread (FAQ ↗)
Python the asyncio event loop ↗ runs asynchronous tasks and callbacks, network I/O and subprocesses
C# events ↗, built on the delegate model and declared with the event keyword
JavaScript the host's event loop takes jobs from a job queue (execution model ↗); handlers are attached with addEventListener, or with Node's EventEmitter
The operating system epoll ↗ reports which of many file descriptors can do I/O, edge- or level-triggered

Where to read more