From e15ebada4d1e91992f93ed53ca6027733fd64844 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Schm=C3=B6lder?= Date: Mon, 28 Aug 2023 15:47:21 +0200 Subject: [PATCH] Improve cached_properties_when_locked --- CADETProcess/dataStructure/cache.py | 11 ++++++++++- CADETProcess/simulator/cadetAdapter.py | 27 ++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/CADETProcess/dataStructure/cache.py b/CADETProcess/dataStructure/cache.py index 2cff30eec..b1ff53300 100644 --- a/CADETProcess/dataStructure/cache.py +++ b/CADETProcess/dataStructure/cache.py @@ -23,8 +23,17 @@ def name(self): class CachedPropertiesMixin(Structure): - lock = Bool(default=False) + _lock = Bool(default=False) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.cached_properties = {} + + @property + def lock(self): + return self._lock + + @lock.setter + def lock(self, lock): + self._lock = lock + self.cached_properties = {} diff --git a/CADETProcess/simulator/cadetAdapter.py b/CADETProcess/simulator/cadetAdapter.py index 8f875a1df..d40abce24 100644 --- a/CADETProcess/simulator/cadetAdapter.py +++ b/CADETProcess/simulator/cadetAdapter.py @@ -1,4 +1,5 @@ from collections import defaultdict +from functools import wraps import os import platform from pathlib import Path @@ -122,6 +123,25 @@ def temp_dir(self): def temp_dir(self, temp_dir): self._temp_dir = temp_dir + def locks_process(func): + """Lock process to enable caching.""" + @wraps(func) + def wrapper(self, process, *args, **kwargs): + locked_process = False + + if not process.lock: + process.lock = True + locked_process = True + + results = func(self, process, *args, **kwargs) + + if locked_process: + process.lock = False + + return results + + return wrapper + def autodetect_cadet(self): """ Autodetect installation CADET based on operating system and API usage. @@ -356,6 +376,7 @@ def create_lwe(self, file_path=None): return cadet_model + @locks_process def run(self, process, cadet=None, file_path=None): """Interface to the solver run function. @@ -464,6 +485,7 @@ def load_from_h5(self, file_path): return cadet + @locks_process def get_process_config(self, process): """Create the CADET config. @@ -484,16 +506,12 @@ def get_process_config(self, process): get_input_sensitivity """ - process.lock = True - config = Dict() config.input.model = self.get_input_model(process) config.input.solver = self.get_input_solver(process) config.input['return'] = self.get_input_return(process) config.input.sensitivity = self.get_input_sensitivity(process) - process.lock = False - return config def load_simulation_results(self, process, file_path): @@ -502,6 +520,7 @@ def load_simulation_results(self, process, file_path): return results + @locks_process def get_simulation_results( self, process,