python for-loop parallelization using multiprocessing.pool -
i have piece of code looks this:
def calc_stuff(x,a,b,c): ... return y x = range(n) y = zeros(x.shape) if __name__ == '__main__': p = pool(nprocs) y = p.map(calc_stuff,x,a,b,c)
this not work, , searched online, because map function deals iterables rather argument lists. wonder what's simplest method modify code parallelize it, i.e., x array/iterable want parallelize.
thank you.
one option use itertools.repeat
zip
(or itertools.izip
) build multiple arguments iterable of tuples , use multiprocessing.pool.starmap
call function tuple unpacked arguments:
from itertools import repeat if __name__ == '__main__': p = pool(nprocs) y = p.starmap(calc_stuff, zip(x, repeat(a), repeat(b), repeat(c)))
Comments
Post a Comment