Skip to content

Instantly share code, notes, and snippets.

@kaspermunch
Created October 19, 2020 11:55
Show Gist options
  • Select an option

  • Save kaspermunch/64e11cd21e3953295e149e75bee51733 to your computer and use it in GitHub Desktop.

Select an option

Save kaspermunch/64e11cd21e3953295e149e75bee51733 to your computer and use it in GitHub Desktop.
How to easily use multiprocessing in Python (on a SLURM cluster)
import os
from multiprocessing import Pool, cpu_count
# function you want to run in parallel:
def myfunction(a, b):
return a + b
# list of tuples to serve as arguments to function:
args = [(1, 2), (9, 11), (6, 2)]
# number of cores you have allocated for your slurm task:
number_of_cores = int(os.environ['SLURM_CPUS_PER_TASK'])
# number_of_cores = cpu_count() # if not on the cluster you should do this instead
# multiprocssing pool to distribute tasks to:
with Pool(number_of_cores) as pool:
# distribute computations and collect results:
results = pool.starmap(myfunction, args)
@kaspermunch
Copy link
Author

kaspermunch commented Jun 17, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment