我想在多处理中应用这个函数:
import multiprocessing
import numpy as np
a = np.random.randint(1,4, size=(10,10,10))
def function(input_field,[num1,num2,num3]):
pass
if __name__ == '__main__':
pool = multiprocessing.Pool(processes=4)
test_list = [(a,[27.5, 27.5, 25]), (b,[27.5, 27.5, 25]), (c,[27.5, 27.5, 25]), (d,[27.5, 27.5, 25])]
results = pool.map(function, test_list)
pool.close()
pool.join()
如何将function(input_field,[num1,num2,num3])
应用于多处理?
根据test_list
的结构,您有 2 个选项:
-
修改您的函数以支持单个参数并将其解压缩到函数中并继续使用
map
:def function(arg): input_field, l = arg
-
使用
starmap
并修复函数定义,因为test_list
的结构是starmap
期望得到的:def function(input_field, l): ... results = pool.starmap(function, test_list)