@@ -250,7 +250,7 @@ def setTempDirectory(self, customBuildDirectory):
250
250
def getWorkDirectory (self ):
251
251
return self .tempdir
252
252
253
- def _run_cmd (self , cmd : list ):
253
+ def _run_cmd (self , cmd : list , timeout : Optional [ int ] = None ):
254
254
logger .debug ("Run OM command %s in %s" , cmd , self .tempdir )
255
255
256
256
if platform .system () == "Windows" :
@@ -273,13 +273,16 @@ def _run_cmd(self, cmd: list):
273
273
my_env = None
274
274
275
275
try :
276
- cmdres = subprocess .run (cmd , capture_output = True , text = True , env = my_env , cwd = self .tempdir )
276
+ cmdres = subprocess .run (cmd , capture_output = True , text = True , env = my_env , cwd = self .tempdir ,
277
+ timeout = timeout )
277
278
stdout = cmdres .stdout .strip ()
278
279
stderr = cmdres .stderr .strip ()
279
280
if cmdres .returncode != 0 or stderr :
280
281
raise ModelicaSystemError (f"Error running command { cmd } : { stderr } " )
281
282
if self ._verbose and stdout :
282
283
logger .info ("OM output for command %s:\n %s" , cmd , stdout )
284
+ except subprocess .TimeoutExpired :
285
+ raise ModelicaSystemError (f"Timeout running command { repr (cmd )} " )
283
286
except Exception as e :
284
287
raise ModelicaSystemError (f"Exception { type (e )} running command { cmd } : { e } " )
285
288
@@ -575,7 +578,7 @@ def get_exe_file(self) -> pathlib.Path:
575
578
else :
576
579
return pathlib .Path (self .tempdir ) / self .modelName
577
580
578
- def simulate (self , resultfile = None , simflags = None ): # 11
581
+ def simulate (self , resultfile = None , simflags = None , timeout : Optional [ int ] = None ): # 11
579
582
"""
580
583
This method simulates model according to the simulation options.
581
584
usage
@@ -638,7 +641,7 @@ def simulate(self, resultfile=None, simflags=None): # 11
638
641
639
642
cmd = exe_file .as_posix () + override + csvinput + r + simflags
640
643
cmd = cmd .split (" " )
641
- self ._run_cmd (cmd = cmd )
644
+ self ._run_cmd (cmd = cmd , timeout = timeout )
642
645
self .simulationFlag = True
643
646
644
647
# to extract simulation results
@@ -1015,7 +1018,8 @@ def optimize(self): # 21
1015
1018
1016
1019
return optimizeResult
1017
1020
1018
- def linearize (self , lintime : Optional [float ] = None , simflags : Optional [str ] = None ) -> LinearizationResult :
1021
+ def linearize (self , lintime : Optional [float ] = None , simflags : Optional [str ] = None ,
1022
+ timeout : Optional [int ] = None ) -> LinearizationResult :
1019
1023
"""Linearize the model according to linearOptions.
1020
1024
1021
1025
Args:
@@ -1076,7 +1080,7 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
1076
1080
else :
1077
1081
cmd = exe_file .as_posix () + linruntime + override + csvinput + simflags
1078
1082
cmd = cmd .split (' ' )
1079
- self ._run_cmd (cmd = cmd )
1083
+ self ._run_cmd (cmd = cmd , timeout = timeout )
1080
1084
1081
1085
# code to get the matrix and linear inputs, outputs and states
1082
1086
linearFile = pathlib .Path (self .tempdir ) / "linearized_model.py"
0 commit comments