Can't pickle when using multiprocessing Pool.map()

后端 未结 12 1914
醉梦人生
醉梦人生 2020-11-22 00:19

I\'m trying to use multiprocessing\'s Pool.map() function to divide out work simultaneously. When I use the following code, it works fine:

12条回答
  •  不要未来只要你来
    2020-11-22 01:00

    The solution from parisjohn above works fine with me. Plus the code looks clean and easy to understand. In my case there are a few functions to call using Pool, so I modified parisjohn's code a bit below. I made call to be able to call several functions, and the function names are passed in the argument dict from go():

    from multiprocessing import Pool
    class someClass(object):
        def __init__(self):
            pass
    
        def f(self, x):
            return x*x
    
        def g(self, x):
            return x*x+1    
    
        def go(self):
            p = Pool(4)
            sc = p.map(self, [{"func": "f", "v": 1}, {"func": "g", "v": 2}])
            print sc
    
        def __call__(self, x):
            if x["func"]=="f":
                return self.f(x["v"])
            if x["func"]=="g":
                return self.g(x["v"])        
    
    sc = someClass()
    sc.go()
    

提交回复
热议问题