from multiprocessing import Pool
def cube(x):
return x ** 3
if __name__ == "__main__":
pool = Pool(5)
result = pool.map(cube, [0, 1, 2, 3])
Pool
is a class which manages multiple Workers
(processes) behind the scenes and lets you, the programmer, use.
Pool(5)
creates a new Pool with 5 processes, and pool.map
works just like map but it uses multiple processes (the amount defined when creating the pool).
Similar results can be achieved using map_async
, apply
and apply_async
which can be found in the documentation.