Python multiprocessing pool map with multiple arguments [duplicate]

谁都会走 提交于 2019-12-23 02:59:15

问题


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

but it gives me following error

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

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)


回答1:


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


来源:https://stackoverflow.com/questions/29427460/python-multiprocessing-pool-map-with-multiple-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!