将** kwargs直接传递到多处理程序的Pool时如何处理?

时间:2019-09-06 14:27:25

标签: python python-multiprocessing

我想将kwargs传递给multiprocessing Pool。将kwargs传递给该函数可以,但是不能使用多处理。

在这种情况下如何使用kwargs

这是我的代码:

from sklearn.model_selection import ParameterGrid
import multiprocessing
from multiprocessing import Pool
from multiprocessing import cpu_count

abc = [13, 15]
xyz = [2,3,5]

parameterGridDic = [{
    'abc': abc,
    'xyz': xyz
}]

def myFunc(**kwargs):
    return kwargs['xyz'], kwargs['abc']

if __name__ == '__main__':
    multiprocessing.freeze_support()

    parameterGrid = ParameterGrid(parameterGridDic)
    parameterGridList = list(parameterGrid)

    for parameterGridEntry in parameterGridList:
        myResult = myFunc(**parameterGridEntry)
        print("myResult:", myResult)


    pool = Pool(cpu_count())
    myResult2 = pool.map(myFunc,**parameterGridList)

    print("myResult2:", myResult2)

这将产生以下输出:

myResult: (2, 13)
myResult: (3, 13)
myResult: (5, 13)
myResult: (2, 15)
myResult: (3, 15)
myResult: (5, 15)

...

    myResult2 = pool.map(myFunc,**parameterGridList)
TypeError: map() argument after ** must be a mapping, not list

0 个答案:

没有答案