具有多个参数的 Python 多处理池映射 [英] Python multiprocessing pool map with multiple arguments

查看:37
本文介绍了具有多个参数的 Python 多处理池映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数可以从 multiprocessing pool.map 中使用多个参数调用.

I have a function to be called from multiprocessing pool.map with multiple arguments.

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)

I have tried solutions from other answers here but they are not working for me. How can i resolve it and what is the reason for this problem (I did not get the pickle POV)

推荐答案

你应该在数组中给出参数

You should be giving args in array

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()

这篇关于具有多个参数的 Python 多处理池映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆