Intermediate
Short answer
Free
Threads vs Processes
Compare threads and processes across memory, communication cost, and crash isolation. When would you deliberately choose multiple processes over multiple threads?
Solution
| Property | Process | Thread |
|---|---|---|
| Memory | Private, isolated address space | Shared within the process |
| Communication | Explicit IPC (pipes, sockets, queues, shared memory) | Shared variables (needs locks) |
| Creation cost | High | Low |
| Crash isolation | A crash is contained to that process | A crash can take down the whole process |
When to choose processes over threads:
- CPU-bound work in Python. Because of the GIL, threads cannot run
Python bytecode in parallel. Separate processes each have their own GIL,
giving true multi-core parallelism (
multiprocessing). - Fault isolation. If you need one unit of work to be unable to corrupt or crash another (e.g. running untrusted plugins), process isolation is far stronger than thread isolation.
- Independent scaling / deployment. Separate processes can be restarted or scaled independently.
The trade-off is that inter-process communication is more expensive and explicit than sharing memory between threads.