1 回答
TA贡献1772条经验 获得超5个赞
您可以使用期望元组的元组,这些元组是函数的参数。在这里,为了显示步骤,我提前生成了元组并将其存储在func_arg_tuple中。pool.starmap
此外,无需在data_generation函数中保存结果的中间步骤。只需返回要处理的值,当池的工作完成时,它将在值中。results
import multiprocessing
def data_generation(param1, param2, param3):
# sample processing function - add your own
return np.random.normal(param3, param1, param2)
param1 = 1
param2 = 2
with multiprocessing.Pool(processes=multiprocessing.cpu_count()) as pool:
func_arg_tuple = tuple((param1, param2, ratio) for ratio in np.arange(0.3, 1.4, 0.1))
results = pool.starmap(data_generation, func_arg_tuple)
>>>results
[array([-0.89459092, 2.03727378]),
array([-0.79459092, 2.13727378]),
array([-0.69459092, 2.23727378]),
array([0.57145244, 0.75501116]),
array([ 2.40776746, -0.17882601]),
array([0.77145244, 0.95501116]),
array([-0.88291467, 1.3072801 ]),
array([0.97145244, 1.15501116]),
array([0.0123457 , 2.25667152]),
array([2.90776746, 0.32117399]),
array([3.00776746, 0.42117399])]
添加回答
举报