具有不同参数的函数的并行处理

时间:2019-06-05 10:57:00

标签: python parallel-processing

所以我只想尝试一些并行化来加快脚本速度,但是我发现的所有内容并不能真正帮助我。

我正在运行优化算法,并且在每个迭代中,都有必要对不同参数进行多次评估。每个评估都是一个模拟,大约需要10分钟。

while(True):
    f_xn = runSim(xn[0],xn[1],25,na)
    f_xb = runSim(xb[0],xb[1],25,na)
    if f_xn < e:
        break

那么,有没有一种方法可以让我同时运行这两个函数调用,然后让它们在值f_xnf_xb都完成时继续执行呢?

runSim()的定义。这里缺少一些行,但是这些行只是在更改文本文件中的某些值。

def runSim(n):
    if 'SUMO_HOME' in os.environ:
        tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
        sys.path.append(tools)
    else:   
         sys.exit("please declare environment variable 'SUMO_HOME'")

    sumoBinary = "C:\Program Files (x86)/DLR/Sumo/bin/sumo-gui.exe"
    sumoCmd = [sumoBinary, "-c", "sim/sumo_config.sumo.cfg"]

    speed_error = []
    ntg_error = []

    for i in range(0,n):
        print(i)
        sumoBinary = "C:\Program Files (x86)/DLR/Sumo/bin/sumo.exe"
        sumoCmd = [sumoBinary, "-c", "sim/sumo_config.sumo.cfg"]
        traci.start(sumoCmd) 
        while traci.simulation.getMinExpectedNumber() > 0:
           traci.simulationStep()
           setVehType()

        traci.close()
        speed, ntg = Auswertung.auswerten()
        speed_error.append(speed)
        ntg_error.append(ntg)

    speed_mean_error = sum(speed_error)/len(speed_error)
    ntg_mean_error = sum(ntg_error)/len(ntg_error)    
    return speed_mean_error, ntg_mean_error

1 个答案:

答案 0 :(得分:0)

遵循此documentation,以下代码即可:

from multiprocessing import Pool

pool = Pool(processes=2)              # start worker processes

while True:
    # launching multiple evaluations asynchronously
    p1 = pool.apply_async(runSim, (xn[0],xn[1],25,na))
    p2 = pool.apply_async(runSim, (xb[0],xb[1],25,na))
    f_xn = p1.get()
    f_xb = p2.get()
    if f_xn < e:
        break