I'm new to multiprocessing (in any language), and I'm learning how to implement it particularly in Python. A typical example given is something such as:
import multiprocessing as mp
def compute(x):
return x * x
if __name__ == '__main__':
with mp.Pool() as p:
res = p.map(compute, [1, 2, 3, 4])
print(res) # prints [1, 4, 9, 16]
To me, it seems like the function compute
does x=1
first, followed by x=2
, then x=3
, and finally x=4
before terminating and returning the result. Or does the above code simultaneously call compute
in separate cores for x = 1, 2, 3, and 4 (i.e., four separate compute
calls)?
Granted, this example is very trivial and an overkill for multiprocessing, but I will any clarification about what Pool
does. Thank you for any pointers.