具有多个参数的Python多处理池映射

2024-05-19 20:53:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个函数要用多个参数从多处理pool.map调用。

from multiprocessing import Pool
import time

def printed(num,num2):
    print 'here now '
    return num

class A(object):
    def __init__(self):
        self.pool = Pool(8)

    def callme(self):
        print self.pool.map(printed,(1,2),(3,4))
if __name__ == '__main__':
    aa = A()
    aa.callme()

但它给了我以下的错误

TypeError: printed() takes exactly 2 arguments (1 given)

我在这里尝试过其他答案的解决方案,但它们对我不起作用。 我怎样才能解决这个问题?这个问题的原因是什么(我没有得到泡菜POV)


Tags: 函数fromimportselfmap参数defmultiprocessing
1条回答
网友
1楼 · 发布于 2024-05-19 20:53:45

你应该在数组中给出参数

from multiprocessing import Pool
import time

def printed(*args):
    print 'here now '
    return args[0][0]

class A(object):
    def __init__(self):
        self.pool = Pool(8)

    def callme(self):
        print self.pool.map(printed,[(1,2),(3,4)])
if __name__ == '__main__':
    aa = A()
    aa.callme()

相关问题 更多 >