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

后端 未结 12 2005
醉梦人生
醉梦人生 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:07

    In this simple case, where someClass.f is not inheriting any data from the class and not attaching anything to the class, a possible solution would be to separate out f, so it can be pickled:

    import multiprocessing
    
    
    def f(x):
        return x*x
    
    
    class someClass(object):
        def __init__(self):
            pass
    
        def go(self):
            pool = multiprocessing.Pool(processes=4)       
            print pool.map(f, range(10))
    

提交回复
热议问题