How do I avoid this pickling error, and what is the best way to parallelize this code in Python?

后端 未结 1 1753
名媛妹妹
名媛妹妹 2020-12-19 05:14

I have the following code.

def main():
  (minI, maxI, iStep, minJ, maxJ, jStep, a, b, numProcessors) = sys.argv
  for i in range(minI, maxI, iStep):
    for         


        
相关标签:
1条回答
  • 2020-12-19 05:37

    The reason you are most likely seeing this behavior is because of the order in which you define your pool, objects, and functions. multiprocessing is not quite the same as using threads. Each process will spawn and load a copy of the environment. If you create functions in scopes that may not be available to the processes, or create objects before the pool, then the pool will fail.

    First, try creating one pool before your big loop:

    (minI, maxI, iStep, minJ, maxJ, jStep, a, b, numProcessors) = sys.argv
    pool = multiprocessing.Pool(processes=numProcessors)
    for i in range(minI, maxI, iStep):
        ...
    

    Then, move your target callable outside the dynamic loop:

    def functionB(a, b):
        ...
    
    def main():
        ...
    

    Consider this example...

    broken

    import multiprocessing
    
    def broken():
        vals = [1,2,3]
    
        def test(x):
            return x
    
        pool = multiprocessing.Pool()
        output = pool.map(test, vals)
        print output
    
    broken()
    # PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
    

    working

    import multiprocessing
    
    def test(x):
        return x
    
    def working():
        vals = [1,2,3]
    
        pool = multiprocessing.Pool()
        output = pool.map(test, vals)
        print output
    
    working()
    # [1, 2, 3]
    
    0 讨论(0)
提交回复
热议问题