Process vs Thread
Process vs Thread: Deep Architectural Comparison
Choosing the appropriate concurrency model is one of the most critical engineering decisions in systems programming. In Python, the choice between Threads (threading) and Processes (multiprocessing) impacts memory footprint, throughput, fault tolerance, and developer ergonomics.
1. Operating System Mechanics: Process vs Thread
At the OS kernel level, processes and threads represent different levels of isolation:
Key Differences
| Metric | Thread (threading) | Process (multiprocessing) |
|---|---|---|
| Memory Isolation | Shared heap, global variables, and open file descriptors | Completely isolated address space; memory is private |
| Context Switching | Low overhead: CPU only swaps registers and stack pointers | High overhead: OS must flush CPU TLB caches and swap page tables |
| Data Sharing | Direct pointers in shared RAM (requires mutex locks) | Inter-Process Communication (IPC): requires pickling/serialization |
| Crash Isolation | Zero: A segfault in any thread kills the entire process | High: A crash in a worker process leaves parent and siblings running |
| CPython GIL Impact | Bound by the single GIL; cannot utilize multiple CPU cores | Each process has its own GIL; full multi-core CPU parallelism |
| Startup Cost | Low (~microsecond scale) | High (~millisecond scale; requires spawning or forking an interpreter) |
2. Quantitative Benchmark: CPU-Bound vs I/O-Bound
The table below summarizes the performance behavior of threads vs processes in CPython:
3. Fault Tolerance & Failure Isolation
In high-reliability backend systems, fault isolation is a critical consideration. If third-party C-extension code (e.g. OpenCV, TensorFlow, or a legacy C shared library) crashes via a null-pointer dereference or segmentation fault:
- Threaded Model: The entire application (all threads, all user sessions) crashes immediately.
- Multiprocess Model: Only the child worker process dies. The parent supervisor detects the non-zero exit code (
exitcode < 0indicates killed by signal) and can respawn a replacement worker without dropping service availability.
4. Concurrency Decision Matrix
Use this systematic checklist when architecting Python systems:
5. Architectural Summary Table
| Criterion | Threading | Multiprocessing | Asyncio |
|---|---|---|---|
| Best For | Moderate I/O-bound tasks | Heavy CPU-bound computation | Massive concurrent I/O (sockets, web) |
| Concurrency Type | Preemptive multitasking | Preemptive parallel execution | Cooperative single-threaded concurrency |
| Hardware Utilization | 1 CPU core at a time | All available CPU cores | 1 CPU core |
| Data Sharing | Shared heap (use Locks) | IPC / Pipes / SharedMemory | Shared heap (single thread, no data race) |
| Fault Isolation | Poor (shared crash) | High (isolated processes) | Medium (uncaught exception bubbles) |
Multiple Choice Questions
1.
What occurs at the operating system level during a process context switch that makes it significantly more expensive than a thread context switch? A. The computer must be rebooted. B. The OS must switch page table mappings in the Memory Management Unit (MMU) and invalidate the CPU Translation Lookaside Buffer (TLB). C. All disk drives are unmounted. D. The Global Interpreter Lock is uninstalled.
2.
Why is multiprocessing preferred over threading for CPU-intensive mathematical simulations in Python? A. Multiprocessing does not require importing standard libraries. B. Each process runs its own CPython interpreter instance with a private GIL, allowing simultaneous execution across multiple CPU cores. C. Threads cannot execute loops. D. Multiprocessing bypasses operating system security.
3.
In an application utilizing multithreading, what happens if one worker thread encounters a segmentation fault inside a compiled C-extension? A. Only that specific thread exits; other threads continue running. B. The entire operating system process crashes immediately, terminating all threads. C. CPython catches the segmentation fault and turns it into a KeyError. D. The thread restarts automatically.
4.
When would threading (or asyncio) be preferred over multiprocessing for an I/O-bound web crawler? A. When you want to maximize CPU cache thrashing. B. Because threads have lower memory overhead and avoid the serialization (pickling) costs associated with inter-process communication. C. Because processes cannot establish internet connections. D. Because threads run faster on GPUs.
5.
How is data transferred between separate processes in Python's multiprocessing module by default? A. Direct raw pointer dereferencing in memory. B. Data is serialized into byte streams using pickle and transferred via inter-process communication (IPC) pipes or queues. C. Data is written to optical discs. D. Through global environment variables.
pickle protocol and sends the bytes through operating system pipes or sockets.Shared Memory & Queues
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Multiprocessing Basics | Shared Memory & Queues |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.