问题
I would like to create objects of class Training
and create multiple processes which call the print()
function.
I have a class Training
:
class Training():
def __init__(self, param1, param2):
self.param1 = param1
self.param2 = param2
def print(self):
print(self.param1)
print(self.param2)
I have tried to use the starmap
function to create 5 processes in the following way:
import multiprocessing as mp
num_devices = 5
func_args = []
for i in range (0, num_devices):
func_args.append((i, i*10))
with mp.Pool(num_devices) as pool:
obj = pool.starmap(Training, func_args, chunksize=1)
obj[0].print()
obj[1].print()
obj[2].print()
obj[3].print()
obj[4].print()
However, this code is is creating multiple processes to create the objects, and not to run the print()
function. How can I do it in the correct way?
回答1:
Create a list of objects initialised with the required parameters, and a helper function to call the method of object:
def helper_func(obj):
obj.print()
func_args = []
for i in range (0, num_devices):
obj = Training(i, i*10)
func_args.append(obj)
with mp.Pool(num_devices) as pool:
pool.map(helper_func, func_args, chunksize=1)
来源:https://stackoverflow.com/questions/63016941/multiprocessing-for-creating-objects-calling-functions-using-starmap-python