Skip to content

Commit 69728a9

Browse files
committed
[ModelicaSystem] add timeout to subprocess.run() in _run_cmd()
1 parent d8ff798 commit 69728a9

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

OMPython/ModelicaSystem.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def setTempDirectory(self, customBuildDirectory):
250250
def getWorkDirectory(self):
251251
return self.tempdir
252252

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

256256
if platform.system() == "Windows":
@@ -273,13 +273,16 @@ def _run_cmd(self, cmd: list):
273273
my_env = None
274274

275275
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)
277278
stdout = cmdres.stdout.strip()
278279
stderr = cmdres.stderr.strip()
279280
if cmdres.returncode != 0 or stderr:
280281
raise ModelicaSystemError(f"Error running command {cmd}: {stderr}")
281282
if self._verbose and stdout:
282283
logger.info("OM output for command %s:\n%s", cmd, stdout)
284+
except subprocess.TimeoutExpired:
285+
raise ModelicaSystemError(f"Timeout running command {repr(cmd)}")
283286
except Exception as e:
284287
raise ModelicaSystemError(f"Exception {type(e)} running command {cmd}: {e}")
285288

@@ -575,7 +578,7 @@ def get_exe_file(self) -> pathlib.Path:
575578
else:
576579
return pathlib.Path(self.tempdir) / self.modelName
577580

578-
def simulate(self, resultfile=None, simflags=None): # 11
581+
def simulate(self, resultfile=None, simflags=None, timeout: Optional[int] = None): # 11
579582
"""
580583
This method simulates model according to the simulation options.
581584
usage
@@ -638,7 +641,7 @@ def simulate(self, resultfile=None, simflags=None): # 11
638641

639642
cmd = exe_file.as_posix() + override + csvinput + r + simflags
640643
cmd = cmd.split(" ")
641-
self._run_cmd(cmd=cmd)
644+
self._run_cmd(cmd=cmd, timeout=timeout)
642645
self.simulationFlag = True
643646

644647
# to extract simulation results
@@ -1015,7 +1018,8 @@ def optimize(self): # 21
10151018

10161019
return optimizeResult
10171020

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:
10191023
"""Linearize the model according to linearOptions.
10201024
10211025
Args:
@@ -1076,7 +1080,7 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
10761080
else:
10771081
cmd = exe_file.as_posix() + linruntime + override + csvinput + simflags
10781082
cmd = cmd.split(' ')
1079-
self._run_cmd(cmd=cmd)
1083+
self._run_cmd(cmd=cmd, timeout=timeout)
10801084

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

0 commit comments

Comments
 (0)