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 multithreading and serial, it, its arguments and its results are sent to other processes or interpreters, so they must be picklable. For multiprocessing and the MPI modes, f pickles by reference, so it must be defined at the top level of a module: not a lambda nor a closure. dask (through cloudpickle) and multiinterpreter also take a lambda.

  • args – iterables of the arguments of f, as in map(). They are consumed in full before any result is returned.

  • processes – the number of workers; None for the backend’s default. If processes <= 1, run serially (in every MPI rank, for mpi_simple). For mpi, the number of MPI processes is set by mpiexec if the script is run with python -m mpi4py.futures, else it is the number of processes to spawn. For mpi_simple, it is the number of MPI processes, set by mpiexec only.

  • mode –

    the backend to run on:

  • return_results – if False, return [] instead of the results, which are then not sent back (for mpi_simple, not gathered to rank 0). Exceptions raised by f still propagate.

  • chunksize – the number of calls of f sent to a worker at a time. Larger chunks cost less overhead when f is quick; smaller ones balance the load better when its duration varies. Ignored by mpi_simple and serial.

Returns:

the results, in order. For mpi_simple, on rank 0 only; the other ranks get [].

Raises:
  • ValueError – if mode or chunksize is invalid.

  • ImportError – if the backend of mode is not available: dask or mpi4py is not installed, or multiinterpreter on Python < 3.14.

Any exception that f raises 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.