Multiprocessing by the Class Pool of Python
Contemporary PCs support simultaneous multiprocessing, so you can make large calculation efficiently by partitioning the calculation into groups for multiprocessing. With the class Pool of Python, we can employ multiprocessing easily.
Multiprocessing by Pool executes the processes simultaneously in parallel. A simple example is shown in Listing 1 below. The script executes three functions of Listing 2 in parallel, each of which displays the window created by a class Tk of a package tkinter (Figure 1).
Figure 1
These windows will disappear after 10 seconds.
Multiprocessing in Listing 1 is executed by this code:
pool = Pool()
Results = pool.map(func_r,
params)
The above code should be executed under the condition
__name__ == '__main__'
When this condition is ignored, the program will hang up.
The function func_r is declared in the module submodule of Listing 2 below. A function, which is called by map and has only one parameter, will be executed with each of items of the List argument of map. To use more than one parameters, the function func_r has a dictionary parameter. The function func_r selects the function to call according to the value of the dictionary’s key ‘ID’.
The returned value from map is a List consisted of tuples returned by functions executed by multiprocessing (Figure 2).
Figure 2
The scripts of Listings 1 and 2 are archived in a zip file.
Listing 1. Main.py
from multiprocessing.pool import Pool
from submodule import *
print('Started...')
params_t = {'ID': 0, 'r':0, 'g':0,
'b':0}
params = []
for iproc in range(3):
p_t = params_t.copy()
p_t['ID'] = iproc + 1
if iproc == 0:
p_t['r'] = 255
elif iproc == 1:
p_t['g'] = 255
else:
p_t['b'] = 255
params.append(p_t)
if __name__ == '__main__':
pool = Pool()
Results = pool.map(func_r,
params)
print(Results)
Listing 2. Submodule.py
from tkinter import *
import time
def rgb2str(r, g, b):
return
'#{0:0>2x}{1:0>2x}{2:0>2x}'.format(r,g,b)
def func1(r, g, b):
root = Tk()
c = Canvas(root, width =
100, height = 200, background = rgb2str(0, 255, 255))
c.pack()
for i in range(10):
c.create_rectangle(0, 0, 100, 200, outline = rgb2str(r, g, b), fill =
rgb2str(r, g, b))
c.update()
r =
255 - r
g = 255 - g
b =
255 - b
time.sleep(1)
root.destroy()
return 'func1'
def func2(r, g, b):
root = Tk()
c = Canvas(root, width =
200, height = 100, background = rgb2str(0, 255, 255))
c.pack()
for i in range(10):
c.create_rectangle(0, 0, 200, 100, outline = rgb2str(r, g, b), fill =
rgb2str(r, g, b))
c.update()
r =
255 - r
g =
255 - g
b =
255 - b
time.sleep(1)
root.destroy()
return 'func2'
def func3(r, g, b):
root = Tk()
c = Canvas(root, width =
150, height = 150, background = rgb2str(0, 255, 255))
c.pack()
for i in range(10):
c.create_rectangle(0, 0, 150, 150, outline = rgb2str(r, g, b), fill =
rgb2str(r, g, b))
c.update()
r =
255 - r
g =
255 - g
b =
255 - b
time.sleep(1)
root.destroy()
return 'func3'
def func_r( params ):
results = None
if params['ID'] == 1:
results = func1(params['r'], params['g'], params['b'])
elif params['ID'] == 2:
results = func2(params['r'], params['g'], params['b'])
else:
results = func3(params['r'], params['g'], params['b'])
return results, params['ID']