A pool of processes in Python
If your tasks are calls of a Python function, Python’s standard library can run them on a pool of processes, from one script:
#!/usr/bin/env python3
"""Monte Carlo π on a pool of processes, one for each CPU this job may use."""
import os
from concurrent.futures import ProcessPoolExecutor
import pi
if __name__ == "__main__":
workers = len(os.sched_getaffinity(0))
with ProcessPoolExecutor(max_workers=workers) as pool:
hits = sum(pool.map(pi.count_hits, range(1, pi.TASKS + 1)))
pi.report(hits, pi.TASKS * pi.SAMPLES)ProcessPoolExecutor starts the worker processes once, and pool.map hands each of them a seed, gets back the hits, and hands it the next, so Python and NumPy are imported once per worker, not once per task. The results come back in order, and here they are added up directly, so no files are needed in between. The job is the same as the others, with 8 CPUs:
#!/bin/bash
#SBATCH --job-name=pi-futures
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=8
#SBATCH --time=00:05:00
set -eo pipefail
source "$(conda info --base)/etc/profile.d/conda.sh"
conda activate "${CONDA_ENV:-pi}"
# Where pi.py is, for `import pi`.
export PYTHONPATH="$PWD/../pi"
# Every worker is a process with one thread, and there is one worker per CPU.
source ../pi/num-threads.sh 1
python pi_futures.pyThe number of workers is len(os.sched_getaffinity(0)), the number of CPUs this process may run on, which in a job is the CPUs the job was given. os.cpu_count() is the number of CPUs of the whole node, and on a node shared with other jobs, a pool of that size is oversubscription.1
The pool is started under if __name__ == "__main__":. Depending on the platform and the version of Python, worker processes may start by importing your script afresh, and without that line, each of them would start a pool of its own.2
The older interface, multiprocessing.Pool, does the same, with pool.map too. And if your function spends its time in NumPy, or anything else that releases the GIL, ThreadPoolExecutor from the page on threads does it in one process, with nothing copied between processes.
When a task fails
A pool runs your function in other processes, so when a task raises an exception, it is raised in a worker, where you don’t see it. The pool catches it and raises it again in your script, but only when you ask for that task’s result. pool.map returns an iterator of the results, and it is going through it, here with sum, that asks for them: the exception of the first task that failed is raised there, and the tasks not yet started are cancelled.3 So always go through the results. A pool.map(...) whose results nobody reads, or a pool.submit(...) whose future nobody calls .result() on, loses its exceptions without a word, and the script ends as if every task had succeeded.
If you’d rather run every task and collect the failures at the end, than stop at the first one, catch the exception in the task itself, and return it with the task’s input:
def task(seed):
try:
return seed, pi.count_hits(seed), None
except Exception as e:
return seed, None, repr(e)Then every result says which seed it was, and whether it failed, and the script can list the failed ones, to rerun only those.
map_parallel, a package I wrote, is a complete example of this pattern: a drop-in replacement for map, on a pool of processes, of threads, or of MPI ranks, with the care above taken for each of them, so that an exception in any task is raised in your script.
This gives the same π as the baseline, about 8 times faster, and it stops at the edge of the node. The next page is about going beyond it, with the scheduler running the tasks for you.
Footnotes
Since Python 3.13,
os.process_cpu_count()is the same aslen(os.sched_getaffinity(0)), and it is whatProcessPoolExecutoruses when you give it nomax_workers. Before 3.13, the default wasos.cpu_count().↩︎How workers start is the start method. On Linux, it was
forkuntil Python 3.14, which changed the default to one that does not fork the parent, since forking a process that has threads running can deadlock.↩︎And leaving the
withblock, as the exception goes up, waits for the tasks that were already running, so the script stops once they have finished.↩︎