使用ProcessPoolExecutor进行并行处理时,不返回错误

时间:2019-01-22 21:00:42

标签: python python-3.x parallel-processing simulation concurrent.futures

目前,我正在尝试加快仿真速度。我已经使用线程对其进行了尝试,并且可以正常工作。现在,我想尝试使用并行进程来比较这两种方式。对于我来说,使用futures.ProcessPoolExecutor。当我启动脚本时,模拟时间会打印出来(非常低),但是我的程序无法正常运行。通常,它应该生成几个文件,但是不会生成。此外,没有错误消息。我已经在书本和互联网上对此进行了一些研究,但我找不到问题所在。

这是我的代码:

def main(setting):
    cfg_path = generate(settings[setting])
    run_simulation(cfg_path)

if __name__ == '__main__':

    settings = get_wrapper_input_json("Szenarioanalyse.json")
    typ = "processes"
    start = time.perf_counter()
    if typ == "threads":
        with futures.ThreadPoolExecutor(cpu_count()-1) as e:
            e.map(main,settings)
    elif typ == "processes":
        with futures.ProcessPoolExecutor(cpu_count()-1) as e:
            e.map(main,settings)
    else:
        for setting in settings:
            main(setting)

    print("Simulationtime: "+str(time.perf_counter()-1))

1 个答案:

答案 0 :(得分:0)

我已经解决了问题:

settings = get_wrapper_input_json("Szenarioanalyse.json") #get the settings
parameters = {"Threads":True,"Processes":False,"Serial":False}

def simulate(setting):
    cfg_path = generate(settings[setting])
    run_simulation(cfg_path)


if __name__ == '__main__':

    for key,value in parameters.items():
        if key == "Threads" and value == True:
            start_threads = time.perf_counter()
            with futures.ThreadPoolExecutor(10) as e:
                e.map(simulate,settings)
            print("Simulationtime "+key+": "+str(time.perf_counter()-start_threads))
        elif key == "Processes" and value == True:
            start_processes = time.perf_counter()
            pool = multiprocessing.Pool(multiprocessing.cpu_count())
            pool.map(simulate,settings)
            print("Simulationtime "+key+": "+str(time.perf_counter()-start_processes))
        elif key == "Serial" and value == True:
            start_serial = time.perf_counter()
            for setting in settings:
                simulate(setting)
            print("Simulationtime "+key+": "+str(time.perf_counter()-start_serial))

        #save_dataframe("Szenarioanalyse.json")
        #file_management()