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
- Is built on: Callback, Event loop
- Often confused with: Reactive programming
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¶
- In the books: Grokking Concurrency, Kirill Bobrov — ch. 11, 'Event-based concurrency'
- In the books: JavaScript Concurrency, Adam Boduch — ch. 8, 'Evented IO with NodeJS'
- In the books: Asynchronous Programming, Theophilus Edet — ch. 5, 'Real-time Applications and Event-driven Architectures'
- In the books: C++ Reactive Programming, Praseed Pai, Peter Abraham — ch. 1, 'Reactive Programming Model – Overview and History' → 'Event-driven programming model'
- In the books: Async JavaScript, Trevor Burnham — ch. 2, 'Distributing Events' → 'Evented Models'
- In the books: Designing Distributed Systems, Brendan Burns — ch. 8, 'Functions and Event-Driven Processing'
- Reference: Wikipedia: Event-driven programming ↗