Python multiprocessing vs MPI: which should you use?
Updated · Scientifically reviewed by Dinkar Mishra, PhD on
Short answer
Use multiprocessing (or concurrent.futures) to run independent tasks on the cores of one machine: it is part of Python and needs no setup. Use MPI through mpi4py when work must span several nodes or when processes must exchange data repeatedly during a computation, as in domain-decomposed simulations. For many independent tasks on a cluster, Slurm job arrays are often simpler than either.
Who this is for
Researchers parallelising Python analysis or simulation code who must choose between single-node and cluster-scale approaches.
Software
Python ≥ 3.9mpi4py ≥ 3.1
At a glance
| multiprocessing / concurrent.futures | MPI (mpi4py) | |
|---|---|---|
| Scope | One machine | One machine to thousands of nodes |
| Model | Pool of worker processes; tasks in, results out | Fixed set of ranks running the same program (SPMD), exchanging messages |
| Communication | Pickled arguments and results via the parent | Point-to-point and collective operations; zero-copy for NumPy buffers |
| Setup | Built into Python | Needs an MPI library and the mpi4py package; launched with mpirun/srun |
| Best for | Independent tasks: file processing, parameter sweeps, Monte Carlo | Domain decomposition, iterative solvers, simulations with halo exchange |
| Failure handling | Easy to retry individual tasks | One failed rank usually aborts the whole job |
| Learning curve | Low | Moderate |
The same task both ways
Process 100 input files in parallel. With concurrent.futures, on one node:
from concurrent.futures import ProcessPoolExecutor
from pathlib import Path
def analyse(path):
data = path.read_bytes()
return path.name, len(data)
if __name__ == "__main__":
files = sorted(Path("data").glob("*.h5"))
with ProcessPoolExecutor() as pool: # one worker per CPU core by default
for name, size in pool.map(analyse, files):
print(name, size)
With mpi4py, across nodes. Each rank takes every size-th file, and the results are gathered on rank 0:
from pathlib import Path
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank, size = comm.Get_rank(), comm.Get_size()
files = sorted(Path("data").glob("*.h5"))
mine = [(f.name, len(f.read_bytes())) for f in files[rank::size]]
results = comm.gather(mine, root=0)
if rank == 0:
for name, nbytes in sorted(r for part in results for r in part):
print(name, nbytes)
For truly independent tasks like this, both work. MPI becomes essential when ranks must exchange data during the computation, for example neighbouring subdomains swapping boundary values every time step.
When to choose each
- Choose multiprocessing / concurrent.futures for independent tasks on a workstation or a single cluster node, quick parallelisation of an existing script, and tasks lasting seconds to minutes.
- Choose MPI for computations that need more memory or cores than one node, tightly coupled algorithms with frequent communication, or integration with MPI-based libraries such as PETSc, h5py with parallel HDF5, or simulation codes.
- Choose Slurm job arrays for many independent, long-running tasks on a cluster. See how to submit a Slurm job.
- Before parallelising at all, check whether vectorisation or Numba gives the speedup you need on one core. See how to speed up Python code.
Assumptions and limitations
- Both examples assume a
data/directory of files. They illustrate structure, not I/O performance: parallel jobs reading from one network file system can be limited by storage bandwidth. multiprocessingstart methods differ by platform (fork on Linux by default in older Python versions, spawn on macOS and Windows). Always protect the entry point withif __name__ == "__main__":.- Distributed task frameworks (Dask, Ray) are a middle ground not covered in detail here.
Frequently asked questions
Why not use threads in Python?+
In standard CPython, the global interpreter lock (GIL) prevents threads from executing Python bytecode in parallel. Threads still help for I/O, and for NumPy or other libraries that release the GIL in compiled code. Free-threaded CPython builds are emerging but not yet the norm for scientific stacks.
Can multiprocessing use more than one node?+
No. It starts processes on the local machine only. Frameworks such as Dask or Ray can distribute Python tasks across nodes; MPI is the standard for tightly coupled parallel computations.
Is mpi4py fast enough for real simulations?+
Yes for communication of NumPy arrays: mpi4py's buffer-based methods (capitalised, e.g. comm.Send, comm.Allreduce) pass arrays without pickling, close to native MPI speed. The lowercase methods pickle general Python objects and are slower.
What about a parameter scan with 1000 independent runs?+
On a cluster, a Slurm job array is usually simplest and most robust: each task is a separate job, failures are isolated, and the scheduler fills gaps. Use multiprocessing within a node if each task is very short.
References
- multiprocessing: process-based parallelism · Python documentation
- concurrent.futures: launching parallel tasks · Python documentation
- mpi4py tutorial · mpi4py documentation
- Slurm job arrays · SchedMD
Not sure which to choose?
We benchmark candidate tools on your actual problem and recommend one, independently of any vendor.
Services listed on this page are available today. Products marked “early access” or “in development” on our platform page are not yet generally available.
Spotted an error or an outdated command? Tell us. We correct verified issues promptly.