Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python lambda函数在多进程中不起作用_Python_Python Multiprocessing - Fatal编程技术网

Python lambda函数在多进程中不起作用

Python lambda函数在多进程中不起作用,python,python-multiprocessing,Python,Python Multiprocessing,我正在尝试在多进程中对多个参数使用lambda函数。但是,它不能正常工作。没有运行时错误,但根据activity monitor,CPU不在python上工作。但是,repeat功能工作正常。我的代码如下所示: def testfun(a,b,c,d,e,f): for i in range(100000000): pass return a+b+c+d+e+f def multi_process_tfem(a, b, ksis, w, wn, k):

我正在尝试在多进程中对多个参数使用lambda函数。但是,它不能正常工作。没有运行时错误,但根据activity monitor,CPU不在python上工作。但是,
repeat
功能工作正常。我的代码如下所示:

def testfun(a,b,c,d,e,f):
    for i in range(100000000):
        pass
    return a+b+c+d+e+f

def multi_process_tfem(a, b, ksis, w, wn, k):
    args = ((a, b, ksi, w, wn, k) for ksi in ksis)
    with concurrent.futures.ProcessPoolExecutor() as executor:    
        #results = executor.map(testfun, repeat(a), repeat(b), ksis, repeat(w), repeat(wn), repeat(k)) # working
        results = executor.map(lambda args: testfun(*args), args) # not working
    rho = np.array(list(results))
    return rho
在模块中,您可以使用
lambda
代替
map
。但它似乎没有它。但是,当您尝试在
多处理
中使用
lambda
时,它表明它无法pickle
lambda
函数将其发送到进程,并且在
并发
中也可能出现问题,但它不会显示它


您可以将其作为一个参数,并在函数中解包

def testfun(args):
    a,b,c,d,e,f = args    
    return a+b+c+d+e+f
或者你必须把它定义为普通函数

def unpack_testfun(args):
    return testfun(*args)
并使用它而不是
testfun

executor.map(unpack_testfun, args)

示例代码

import concurrent.futures

def testfun1(args):
    a,b,c,d,e,f = args    
    return a+b+c+d+e+f

def multi_process_tfem1(a, b, ksis, w, wn, k):
    args = ((a, b, ksi, w, wn, k) for ksi in ksis)

    with concurrent.futures.ProcessPoolExecutor() as executor:    
        results = executor.map(testfun1, args) 
    return list(results)

print(multi_process_tfem1(1,1,[1,2,3],1,1,1))

# ---

def testfun2(a,b,c,d,e,f):
    return a+b+c+d+e+f

def unpack(args):
    return testfun2(*args)

def multi_process_tfem2(a, b, ksis, w, wn, k):
    args = ((a, b, ksi, w, wn, k) for ksi in ksis)

    with concurrent.futures.ProcessPoolExecutor() as executor:    
        results = executor.map(unpack, args) 
    return list(results)

print(multi_process_tfem2(1,1,[1,2,3],1,1,1))
在模块中,您可以使用
lambda
代替
map
。但它似乎没有它。但是,当您尝试在
多处理
中使用
lambda
时,它表明它无法pickle
lambda
函数将其发送到进程,并且在
并发
中也可能出现问题,但它不会显示它


您可以将其作为一个参数,并在函数中解包

def testfun(args):
    a,b,c,d,e,f = args    
    return a+b+c+d+e+f
或者你必须把它定义为普通函数

def unpack_testfun(args):
    return testfun(*args)
并使用它而不是
testfun

executor.map(unpack_testfun, args)

示例代码

import concurrent.futures

def testfun1(args):
    a,b,c,d,e,f = args    
    return a+b+c+d+e+f

def multi_process_tfem1(a, b, ksis, w, wn, k):
    args = ((a, b, ksi, w, wn, k) for ksi in ksis)

    with concurrent.futures.ProcessPoolExecutor() as executor:    
        results = executor.map(testfun1, args) 
    return list(results)

print(multi_process_tfem1(1,1,[1,2,3],1,1,1))

# ---

def testfun2(a,b,c,d,e,f):
    return a+b+c+d+e+f

def unpack(args):
    return testfun2(*args)

def multi_process_tfem2(a, b, ksis, w, wn, k):
    args = ((a, b, ksi, w, wn, k) for ksi in ksis)

    with concurrent.futures.ProcessPoolExecutor() as executor:    
        results = executor.map(unpack, args) 
    return list(results)

print(multi_process_tfem2(1,1,[1,2,3],1,1,1))

谢谢你@furas。它现在可以工作了,但是带有*args的lambda函数与外部定义的解包函数是一样的,对吗?你知道为什么只有后一种方法有效吗?正如我在开始时所说的那样,使用
pickle
将函数发送到进程,pickle
lambda
(它创建没有名字的函数)有问题,在
并发
中也可能有问题,另一个潜在的解决方案是
functools.partial
,使用任意参数和KWARG包装函数调用。但是,不要靠近计算机,用concurrent.futures/multiprocessing来测试这一点。谢谢@furas。它现在可以工作了,但是带有*args的lambda函数与外部定义的解包函数是一样的,对吗?你知道为什么只有后一种方法有效吗?正如我在开始时所说的那样,使用
pickle
将函数发送到进程,pickle
lambda
(它创建没有名字的函数)有问题,在
并发
中也可能有问题,另一个潜在的解决方案是
functools.partial
,使用任意参数和KWARG包装函数调用。但是,不在计算机附近使用concurrent.futures/multiprocessing进行测试。