Created
January 27, 2020 17:57
-
-
Save mvidaldp/c1f51d3867723957ace058794998f525 to your computer and use it in GitHub Desktop.
Dummy example of process parallelization in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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