@@ -287,7 +287,7 @@ def setTempDirectory(self, customBuildDirectory):
287
287
def getWorkDirectory (self ):
288
288
return self .tempdir
289
289
290
- def _run_cmd (self , cmd : list ):
290
+ def _run_cmd (self , cmd : list , timeout : Optional [ int ] = None ):
291
291
logger .debug ("Run OM command %s in %s" , cmd , self .tempdir )
292
292
293
293
if platform .system () == "Windows" :
@@ -310,13 +310,16 @@ def _run_cmd(self, cmd: list):
310
310
my_env = None
311
311
312
312
try :
313
- cmdres = subprocess .run (cmd , capture_output = True , text = True , env = my_env , cwd = self .tempdir )
313
+ cmdres = subprocess .run (cmd , capture_output = True , text = True , env = my_env , cwd = self .tempdir ,
314
+ timeout = timeout )
314
315
stdout = cmdres .stdout .strip ()
315
316
stderr = cmdres .stderr .strip ()
316
317
if cmdres .returncode != 0 or stderr :
317
318
raise ModelicaSystemError (f"Error running command { cmd } : { stderr } " )
318
319
if self ._verbose and stdout :
319
320
logger .info ("OM output for command %s:\n %s" , cmd , stdout )
321
+ except subprocess .TimeoutExpired :
322
+ raise ModelicaSystemError (f"Timeout running command { repr (cmd )} " )
320
323
except Exception as e :
321
324
raise ModelicaSystemError (f"Exception { type (e )} running command { cmd } : { e } " )
322
325
@@ -661,7 +664,7 @@ def get_exe_file(self) -> pathlib.Path:
661
664
else :
662
665
return pathlib .Path (self .tempdir ) / self .modelName
663
666
664
- def simulate (self , resultfile = None , simflags = None ): # 11
667
+ def simulate (self , resultfile = None , simflags = None , timeout : Optional [ int ] = None ): # 11
665
668
"""
666
669
This method simulates model according to the simulation options.
667
670
usage
@@ -724,7 +727,7 @@ def simulate(self, resultfile=None, simflags=None): # 11
724
727
725
728
cmd = exe_file .as_posix () + override + csvinput + r + simflags
726
729
cmd = cmd .split (" " )
727
- self ._run_cmd (cmd = cmd )
730
+ self ._run_cmd (cmd = cmd , timeout = timeout )
728
731
self .simulationFlag = True
729
732
730
733
# to extract simulation results
@@ -1041,7 +1044,8 @@ def optimize(self): # 21
1041
1044
1042
1045
return optimizeResult
1043
1046
1044
- def linearize (self , lintime : Optional [float ] = None , simflags : Optional [str ] = None ) -> LinearizationResult :
1047
+ def linearize (self , lintime : Optional [float ] = None , simflags : Optional [str ] = None ,
1048
+ timeout : Optional [int ] = None ) -> LinearizationResult :
1045
1049
"""Linearize the model according to linearOptions.
1046
1050
1047
1051
Args:
@@ -1102,7 +1106,7 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
1102
1106
else :
1103
1107
cmd = exe_file .as_posix () + linruntime + override + csvinput + simflags
1104
1108
cmd = cmd .split (' ' )
1105
- self ._run_cmd (cmd = cmd )
1109
+ self ._run_cmd (cmd = cmd , timeout = timeout )
1106
1110
1107
1111
# code to get the matrix and linear inputs, outputs and states
1108
1112
linearFile = pathlib .Path (self .tempdir ) / "linearized_model.py"
0 commit comments