Skip to content

Commit 26588c7

Browse files
committed
[ModelicaSystem] add timeout to subprocess.run() in _run_cmd()
1 parent 4dc78c2 commit 26588c7

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

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

@@ -669,7 +672,7 @@ def get_exe_file(self) -> pathlib.Path:
669672
else:
670673
return pathlib.Path(self.tempdir) / self.modelName
671674

672-
def simulate(self, resultfile=None, simflags=None): # 11
675+
def simulate(self, resultfile=None, simflags=None, timeout: Optional[int] = None): # 11
673676
"""
674677
This method simulates model according to the simulation options.
675678
usage
@@ -732,7 +735,7 @@ def simulate(self, resultfile=None, simflags=None): # 11
732735

733736
cmd = exe_file.as_posix() + override + csvinput + r + simflags
734737
cmd = [s for s in cmd.split(' ') if s]
735-
self._run_cmd(cmd=cmd)
738+
self._run_cmd(cmd=cmd, timeout=timeout)
736739
self.simulationFlag = True
737740

738741
# to extract simulation results
@@ -1049,7 +1052,8 @@ def optimize(self): # 21
10491052

10501053
return optimizeResult
10511054

1052-
def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = None) -> LinearizationResult:
1055+
def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = None,
1056+
timeout: Optional[int] = None) -> LinearizationResult:
10531057
"""Linearize the model according to linearOptions.
10541058
10551059
Args:
@@ -1110,7 +1114,7 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
11101114
else:
11111115
cmd = exe_file.as_posix() + linruntime + override + csvinput + simflags
11121116
cmd = [s for s in cmd.split(' ') if s]
1113-
self._run_cmd(cmd=cmd)
1117+
self._run_cmd(cmd=cmd, timeout=timeout)
11141118

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

0 commit comments

Comments
 (0)