Python multiprocessing pool map with multiple argu

2019-06-03 18:22发布

This question already has an answer here:

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条回答
我只想做你的唯一
2楼-- · 2019-06-03 18:42

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()
查看更多
登录 后发表回答