Skip to content

Commit

Permalink
Improve cached_properties_when_locked
Browse files Browse the repository at this point in the history
  • Loading branch information
schmoelder committed Sep 9, 2023
1 parent 933eee0 commit e15ebad
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
11 changes: 10 additions & 1 deletion CADETProcess/dataStructure/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
27 changes: 23 additions & 4 deletions CADETProcess/simulator/cadetAdapter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections import defaultdict
from functools import wraps
import os
import platform
from pathlib import Path
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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):
Expand All @@ -502,6 +520,7 @@ def load_simulation_results(self, process, file_path):

return results

@locks_process
def get_simulation_results(
self,
process,
Expand Down

0 comments on commit e15ebad

Please sign in to comment.