Escaping the single thread
The event loop is great for io but bad for heavy computation, since a long calculation blocks everything. Worker threads solve this by running code on a separate thread with its own event loop. The main thread offloads cpu heavy work and stays responsive.
Isolation by default
A worker is not a shared memory free for all. Each worker has its own isolated memory and runtime state.
- Workers do not share ordinary variables with the main thread.
- They communicate by passing messages, copying data across.
- Each worker runs its own event loop, independent of the others.
This isolation avoids most data races that plague shared memory threading, at the cost of copying data between threads.
When to reach for a worker
Use workers for work that would otherwise stall the loop.
- Cpu bound tasks like parsing, hashing, or image processing.
- Parallel pipelines that split a large job across cores.
- Keeping the main loop free for latency sensitive io.
For pure io, the event loop already suffices and a worker would add needless overhead.
Key idea
Worker threads run cpu heavy code on isolated threads with their own event loops, communicating by message passing so the main loop stays responsive without shared memory races.