Skip to content

Commit 175a025

Browse files
committed
one lock per file
1 parent 1146e20 commit 175a025

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

rocketpy/simulation/monte_carlo.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from time import process_time, time
66

77
import h5py
8+
import hdf5plugin
89
import numpy as np
910
import simplekml
1011
from multiprocess import Lock, Process, JoinableQueue
@@ -271,7 +272,9 @@ def _run_in_parallel(self, append, light_mode, n_workers=None):
271272

272273
with MonteCarloManager() as manager:
273274
# initialize queue
274-
write_lock = manager.Lock()
275+
inputs_lock = manager.Lock()
276+
outputs_lock = manager.Lock()
277+
errors_lock = manager.Lock()
275278
sim_counter = manager.SimCounter()
276279
queue = manager.JoinableQueue()
277280

@@ -326,7 +329,9 @@ def _run_in_parallel(self, append, light_mode, n_workers=None):
326329
self.rocket,
327330
self.flight,
328331
sim_counter,
329-
write_lock,
332+
inputs_lock,
333+
outputs_lock,
334+
errors_lock,
330335
queue,
331336
light_mode,
332337
file_paths,
@@ -356,7 +361,9 @@ def _run_simulation_worker(
356361
sto_rocket,
357362
sto_flight,
358363
sim_counter,
359-
write_lock,
364+
inputs_lock,
365+
outputs_lock,
366+
errors_lock,
360367
queue,
361368
light_mode,
362369
file_paths,
@@ -434,13 +441,15 @@ def _run_simulation_worker(
434441
}
435442

436443
# Write flight setting and results to file
437-
write_lock.acquire()
444+
inputs_lock.acquire()
438445
with open(file_paths["input_file"], mode='a', encoding="utf-8") as f:
439446
f.write(json.dumps(inputs_dict, cls=RocketPyEncoder) + "\n")
447+
inputs_lock.release()
448+
449+
outputs_lock.acquire()
440450
with open(file_paths["output_file"], mode='a', encoding="utf-8") as f:
441451
f.write(json.dumps(results, cls=RocketPyEncoder) + "\n")
442-
443-
write_lock.release()
452+
outputs_lock.release()
444453

445454
else:
446455
input_parameters = flightv1_serializer(
@@ -457,14 +466,15 @@ def _run_simulation_worker(
457466
str(sim_idx): flight_results,
458467
}
459468

460-
write_lock.acquire()
469+
inputs_lock.acquire()
461470
with h5py.File(file_paths["input_file"], 'a') as h5_file:
462471
MonteCarlo.dict_to_h5(h5_file, '/', export_inputs)
472+
inputs_lock.release()
463473

474+
outputs_lock.acquire()
464475
with h5py.File(file_paths["output_file"], 'a') as h5_file:
465476
MonteCarlo.dict_to_h5(h5_file, '/', export_outputs)
466-
467-
write_lock.release()
477+
outputs_lock.release()
468478

469479
sim_end = time()
470480

@@ -475,10 +485,10 @@ def _run_simulation_worker(
475485

476486
except Exception as error:
477487
print(f"Error on iteration {sim_idx}: {error}")
478-
write_lock.acquire()
488+
errors_lock.acquire()
479489
with open(file_paths["error_file"], mode='a', encoding="utf-8") as f:
480490
f.write(json.dumps(inputs_dict, cls=RocketPyEncoder) + "\n")
481-
write_lock.release()
491+
errors_lock.release()
482492
raise error
483493

484494
finally:

0 commit comments

Comments
 (0)