By Yuvraj Singh
In this tutorial, we will discuss what is Pool class used in multiprocessing library in python.
What is pool class:
Pool class represents a pool of worker processes. It has methods which allows tasks to be offloaded to the worker processes in a few different ways.
It creates multiple Python processes in the background and spreads out your computations for you across multiple CPU cores so that they all happen in parallel without you needing to do anything. we can even set the number of processes you want to be working at once.
We can use different methods like map, startmap(like map but with multiple arguments), apply etc.
In this tutorial we will learn how to find te square of the list using pool class.
import multiprocessing as mp
return (n * n)
# input list
mylist = [1, 2, 3, 4, 5]
start = time.time()
# creating a pool object
p = mp.Pool()
# map list to target function
result = p.map(square, mylist)
print('time:', time.time() - start)
Submitted by Yuvraj Singh (yuvrajeyes)
Download packets of source code on Coders Packet