如何在循环中使用python多处理Pool.map

时间:2014-03-22 18:54:41

标签: python python-2.7 multiprocessing pool

我正在使用Runge-Kutta进行模拟。每次都需要两个独立变量的步骤二FFT,它们可以并行化。我实现了这样的代码:

from multiprocessing import Pool
import numpy as np

pool = Pool(processes=2)    # I like to calculate only 2 FFTs parallel 
                            # in every time step, therefor 2 processes

def Splitter(args):
    '''I have to pass 2 arguments'''
    return makeSomething(*args):

def makeSomething(a,b):
    '''dummy function instead of the one with the FFT'''
    return a*b

def RungeK():
    # ...
    # a lot of code which create the vectors A and B and calculates 
    # one Kunge-Kutta step for them 
    # ...

    n = 20                         # Just something for the example
    A = np.arange(50000)
    B = np.ones_like(A)

    for i in xrange(n):                  # loop over the time steps
        A *= np.mean(B)*B - A
        B *= np.sqrt(A)
        results = pool.map(Splitter,[(A,3),(B,2)])
        A = results[0]
        B = results[1]

    print np.mean(A)                                 # Some output
    print np.max(B)

if __name__== '__main__':
    RungeK()

不幸的是,python在到达循环后会生成无限数量的进程。在看起来只有两个进程正在运行之前。我的记忆也填满了。添加

pool.close()
pool.join()
循环后面的

并没有解决我的问题,把它放在循环中对我来说毫无意义。希望你能帮忙。

1 个答案:

答案 0 :(得分:2)

将池的创建移动到RungeK函数;

def RungeK():
    # ...
    # a lot of code which create the vectors A and B and calculates
    # one Kunge-Kutta step for them
    # ...

    pool = Pool(processes=2)
    n = 20                         # Just something for the example
    A = np.arange(50000)
    B = np.ones_like(A)

    for i in xrange(n):  # loop over the time steps
        A *= np.mean(B)*B - A
        B *= np.sqrt(A)
        results = pool.map(Splitter, [(A, 3), (B, 2)])
        A = results[0]
        B = results[1]
    pool.close()
    print np.mean(A)  # Some output
    print np.max(B)

或者,将它放在主块中。

这可能是多处理工作方式的副作用。例如。在MS窗口上,您需要能够导入主模块而不会产生副作用(比如创建新进程)。