Skip to content

Instantly share code, notes, and snippets.

@mvidaldp
Created January 27, 2020 17:57
Show Gist options
  • Save mvidaldp/c1f51d3867723957ace058794998f525 to your computer and use it in GitHub Desktop.
Save mvidaldp/c1f51d3867723957ace058794998f525 to your computer and use it in GitHub Desktop.
Dummy example of process parallelization in Python
from multiprocessing import Pool, cpu_count
cores = cpu_count()
# whatever parameters or stuff you want to iterate over to run them on your job (function call)
parameters = [0, 1, 2, 3]
def job(iter):
# the code you want to parallelize
print(iter)
# this iterable list is actually not necessary if no other parameter is passed (so parameters could be used instead)
# but you can pass another parameter by using tuples, for example iterable = [(dataset, param) for param in parameters]
# then you should "unpack" your tuple inside your job function, i.e. iter[0] and iter[1], to use the values in there
iterable = [parameter for parameter in parameters]
pool = Pool(processes=cores)
pool.map(job, iterable)
pool.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment