There's more...

In the previous example, we saw that Pool also provides the map method, which allows us to apply a function to a different set of data. In particular, the scenario in which the same operation is performed in parallel on the elements of the input is referred to as data parallelism.

In the following example, in which we use Pool and map, we create pool with 5 workers and, through the map method, a function of f is applied to a list of 10 elements:

from multiprocessing import Pool

def f(x):
return x+10

if __name__ == '__main__':
p=Pool(processes=5)
print(p.map(f, [1, 2, 3,5,6,7,8,9,10]))

The output is as follows:

11 12 13 14 15 16 17 18 19 20
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset