Skip to content

Commit 78e7ae2

Browse files
committed
[ModelicaSystem] add timeout to subprocess.run() in _run_cmd()
1 parent ba103d7 commit 78e7ae2

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

OMPython/ModelicaSystem.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def setTempDirectory(self, customBuildDirectory):
287287
def getWorkDirectory(self):
288288
return self.tempdir
289289

290-
def _run_cmd(self, cmd: list):
290+
def _run_cmd(self, cmd: list, timeout: Optional[int] = None):
291291
logger.debug("Run OM command %s in %s", cmd, self.tempdir)
292292

293293
if platform.system() == "Windows":
@@ -310,13 +310,16 @@ def _run_cmd(self, cmd: list):
310310
my_env = None
311311

312312
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)
314315
stdout = cmdres.stdout.strip()
315316
stderr = cmdres.stderr.strip()
316317
if cmdres.returncode != 0 or stderr:
317318
raise ModelicaSystemError(f"Error running command {cmd}: {stderr}")
318319
if self._verbose and stdout:
319320
logger.info("OM output for command %s:\n%s", cmd, stdout)
321+
except subprocess.TimeoutExpired:
322+
raise ModelicaSystemError(f"Timeout running command {repr(cmd)}")
320323
except Exception as e:
321324
raise ModelicaSystemError(f"Exception {type(e)} running command {cmd}: {e}")
322325

@@ -661,7 +664,7 @@ def get_exe_file(self) -> pathlib.Path:
661664
else:
662665
return pathlib.Path(self.tempdir) / self.modelName
663666

664-
def simulate(self, resultfile=None, simflags=None): # 11
667+
def simulate(self, resultfile=None, simflags=None, timeout: Optional[int] = None): # 11
665668
"""
666669
This method simulates model according to the simulation options.
667670
usage
@@ -724,7 +727,7 @@ def simulate(self, resultfile=None, simflags=None): # 11
724727

725728
cmd = exe_file.as_posix() + override + csvinput + r + simflags
726729
cmd = cmd.split(" ")
727-
self._run_cmd(cmd=cmd)
730+
self._run_cmd(cmd=cmd, timeout=timeout)
728731
self.simulationFlag = True
729732

730733
# to extract simulation results
@@ -1041,7 +1044,8 @@ def optimize(self): # 21
10411044

10421045
return optimizeResult
10431046

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:
10451049
"""Linearize the model according to linearOptions.
10461050
10471051
Args:
@@ -1102,7 +1106,7 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
11021106
else:
11031107
cmd = exe_file.as_posix() + linruntime + override + csvinput + simflags
11041108
cmd = cmd.split(' ')
1105-
self._run_cmd(cmd=cmd)
1109+
self._run_cmd(cmd=cmd, timeout=timeout)
11061110

11071111
# code to get the matrix and linear inputs, outputs and states
11081112
linearFile = pathlib.Path(self.tempdir) / "linearized_model.py"

0 commit comments

Comments
 (0)