0

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.

cbsteh
  • 809
  • 6
  • 19
  • This will help you https://stackoverflow.com/a/26521507/8366805 – meTchaikovsky Oct 08 '20 at 06:10
  • Have you read the documentation before asking SO to repeat it here? https://docs.python.org/3.8/library/multiprocessing.html#multiprocessing.pool.Pool.map – Grismar Oct 08 '20 at 06:10
  • Does this answer your question? [multiprocessing.Pool: What's the difference between map\_async and imap?](https://stackoverflow.com/questions/26520781/multiprocessing-pool-whats-the-difference-between-map-async-and-imap) – Grismar Oct 08 '20 at 06:11
  • 3
    As you suspect, the code simultaneously calls `compute` in separate cores for each of the values; it limits the number of workers to the parameter passed to the `Pool()` constructor, or the number of CPU cores, though, so if the list has more items, it'll pass them to the workers as they become available. (It also works in chunks, rather than handling single items, to reduce the amount of overhead.) – Jiří Baum Oct 08 '20 at 06:18

0 Answers0