Parallel multiplication of vectors-like computations in python

I have a piece of code like

for i in range(0, len(a))
    b[i] = func(a[i])

where a and b are arrays of the same length, a is given (and large), func is some function that has many local variables, but does not use global variables.

I would like to distribute func calculations on multiple processors. Presumably I need to use the multiprocessing module, but I have not found suitable examples. Could you help me? Thank.

+5
source share
2 answers

Check out the very first example code in multiprocessingdocs :

from multiprocessing import Pool

# you could define `func`, `a` here

if __name__=="__main__":
    p = Pool() # use all available CPU cores
    b = p.map(func, a)
+3
source

. github: https://github.com/mariazverina/codejam/blob/master/src/main.py

from multiprocessing import Pool

p = Pool(4)  # set to number of cores
b = p.map(func, a)
+1

All Articles