multiprocess or threading in python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Python gives you two common ways to run work concurrently: threads and processes. Choosing the right one matters because the wrong model can make a program slower, harder to debug, or much more memory-hungry than necessary.
The core difference
Threads run inside one process and share the same memory space. Processes run as separate operating system processes, so they do not share memory by default.
That difference affects three things immediately:
- how much setup overhead you pay
- whether CPU-bound work can run in parallel
- how safely tasks can share mutable state
In standard CPython, the Global Interpreter Lock means only one thread executes Python bytecode at a time. Because of that, threads are usually best for waiting on external systems, while processes are usually best for heavy computation.
When threading is the better choice
Threading shines for I/O-bound tasks such as:
- calling web APIs
- reading files
- waiting on sockets
- coordinating many slow external operations
While one thread waits, another can continue running. Here is a simple example:
This finishes in about one second rather than two because the waits overlap.
When multiprocessing is the better choice
Multiprocessing is the right default for CPU-bound work such as:
- image processing
- large numerical loops
- parsing huge datasets
- expensive search or optimization tasks
Because each worker is a separate process, the operating system can schedule them on different CPU cores. A small example:
This kind of workload benefits from real parallel execution, something ordinary Python threads cannot provide for pure Python CPU work.
How to decide quickly
A useful rule is simple:
- if your task mostly waits, prefer threads
- if your task mostly computes, prefer processes
There are exceptions. Native libraries such as NumPy may release the GIL, so threads can still help for some numerical workloads. Likewise, processes may be overkill for tiny tasks because starting them and serializing data costs time.
Data sharing is another major factor. Threads share objects directly, which is convenient but risky. Processes isolate state, which is safer but means data must be copied or passed through queues, pipes, or shared memory.
Communication patterns matter
With threads, you often coordinate with locks, queues, and events:
With processes, the safest model is similar: use message passing instead of shared mutable state whenever possible.
Common Pitfalls
One common mistake is using threads for CPU-bound loops and expecting all cores to be busy. In CPython, that usually does not happen because of the GIL.
Another mistake is forgetting the if __name__ == "__main__": guard when using multiprocessing. On platforms that use spawn semantics, missing that guard can recursively start new child processes.
Shared state is another source of bugs. Threads can corrupt data without proper locking, while processes can silently become slow if you pass large objects back and forth too often.
Finally, do not optimize too early. Concurrency adds complexity, so measure first and confirm whether the bottleneck is I/O or CPU time.
Summary
- Threads are usually best for I/O-bound work.
- Processes are usually best for CPU-bound work.
- The GIL limits parallel execution of Python bytecode in threads.
- Processes add startup and serialization overhead but allow true multi-core execution.
- Choose a communication model early and prefer queues or message passing over ad hoc shared state.
Related reading
- Multiprocessing causes Python to crash and gives an error may have been in progress in another thread when fork was called
- Multiprocessing How to use Pool.map on a function defined in a class?
- Multiprocessing on a model with data frame as input
- Multiprocessing or Multithreading?
- Multiprocessing example giving AttributeError in Jupyter Notebook
- multiprocessing How do I share a dict among multiple processes?
- Multiprocessing scikit-learn
- Multiprocessing use tqdm to display a progress bar
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.