map_parallel package¶
A drop-in replacement of map() and itertools.starmap(), but in parallel with different backends.
>>> from map_parallel import map_parallel, starmap_parallel
>>> map_parallel(pow, [1, 2, 3], [2, 2, 2], mode="multithreading")
[1, 4, 9]
>>> starmap_parallel(pow, [(1, 2), (2, 2), (3, 2)], mode="multiprocessing", processes=2)
[1, 4, 9]
Exceptions raised by the mapped function propagate to the caller, whether the results are returned or not:
>>> map_parallel(int, ["1", "x"], mode="multithreading", return_results=False)
Traceback (most recent call last):
...
ValueError: invalid literal for int() with base 10: 'x'
- map_parallel.MODES: tuple[str, ...] = ('multiprocessing', 'multithreading', 'multiinterpreter', 'dask', 'mpi', 'mpi_simple', 'serial')¶
The valid values of
mode.
- map_parallel.map_parallel(f: Callable[[...], T], *args: Iterable[Any], processes: int | None = None, mode: Literal['multiprocessing', 'multithreading', 'multiinterpreter', 'dask', 'mpi', 'mpi_simple', 'serial'] = 'multiprocessing', return_results: bool = True, chunksize: int = 1) list[T][source]¶
Equivalent to
list(map(f, *args)), but in parallel.- Parameters:
f – the function to apply. Except for
multithreadingandserial, it, its arguments and its results are sent to other processes or interpreters, so they must be picklable. Formultiprocessingand the MPI modes,fpickles by reference, so it must be defined at the top level of a module: not a lambda nor a closure.dask(through cloudpickle) andmultiinterpreteralso take a lambda.args – iterables of the arguments of
f, as inmap(). They are consumed in full before any result is returned.processes – the number of workers;
Nonefor the backend’s default. Ifprocesses <= 1, run serially (in every MPI rank, formpi_simple). Formpi, the number of MPI processes is set bympiexecif the script is run withpython -m mpi4py.futures, else it is the number of processes to spawn. Formpi_simple, it is the number of MPI processes, set bympiexeconly.mode –
the backend to run on:
multiprocessing:concurrent.futures.ProcessPoolExecutor, for CPU-bound work in pure Python on a build with a GIL.multithreading:concurrent.futures.ThreadPoolExecutor, for I/O-bound work, work that releases the GIL (much of NumPy), or any work on a free-threaded build of Python.multiinterpreter:concurrent.futures.InterpreterPoolExecutor, Python 3.14 or later: one interpreter per worker, each with its own GIL, in one process.dask: dask.distributed. Runs on the activedistributed.Clientif there is one (processesis then ignored), else on adistributed.LocalClusterofprocessessingle-threaded workers, closed afterwards.mpi:mpi4py.futures.MPIPoolExecutor. May not work, depending on your MPI implementation; run withmpiexec -n N python -m mpi4py.futures script.py.mpi_simple:mpi4py, splitting the work into equal contiguous blocks, one per MPI rank. Every rank must call it with the same arguments; run withmpiexec -n N python script.py.serial:map().
return_results – if
False, return[]instead of the results, which are then not sent back (formpi_simple, not gathered to rank 0). Exceptions raised byfstill propagate.chunksize – the number of calls of
fsent to a worker at a time. Larger chunks cost less overhead whenfis quick; smaller ones balance the load better when its duration varies. Ignored bympi_simpleandserial.
- Returns:
the results, in order. For
mpi_simple, on rank 0 only; the other ranks get[].- Raises:
ValueError – if
modeorchunksizeis invalid.ImportError – if the backend of
modeis not available: dask or mpi4py is not installed, ormultiinterpreteron Python < 3.14.
Any exception that
fraises is re-raised, with the remote traceback as its cause where the backend provides it.
- map_parallel.starmap_parallel(f: Callable[[...], T], args: Iterable[Iterable[Any]], processes: int | None = None, mode: Literal['multiprocessing', 'multithreading', 'multiinterpreter', 'dask', 'mpi', 'mpi_simple', 'serial'] = 'multiprocessing', return_results: bool = True, chunksize: int = 1) list[T][source]¶
Equivalent to
list(itertools.starmap(f, args)), but in parallel.- Parameters:
args – an iterable of iterables, each the arguments of one call of
f.
See
map_parallel()for the other parameters.