Currently i am trying to accelerate my simulation. I have already tried it with threading and it worked. Now i want to try it with parallel processes to compare both ways. That for i use futures.ProcessPoolExecutor. When i start my script the simulationtime gets printed (its very low) but my program does not work as it should. Normally it should generate several files but they are not generated. Furthermore there is no error message. I already did some research on it in books and on the internet but i can´t figure out the problem.
Here is my code:
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))
I have solved the problem:
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()