Skip to content

Commit 083c351

Browse files
authored
Omc process wsl (OpenModelica#302)
* [OMCProcessWSL] (untested) WSL based OMPython with OMC via ZMQ * [OMCProcessWSL] set omc location via argument wsl_omc
1 parent f9bfcf6 commit 083c351

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

OMPython/OMCSession.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,3 +972,79 @@ def _omc_docker_start(self) -> Tuple[subprocess.Popen, DummyPopen]:
972972
f"/ {self._dockerCid}. Log-file says:\n{self.get_log()}")
973973

974974
return omc_process, docker_process
975+
976+
977+
class OMCProcessWSL(OMCProcess):
978+
979+
def __init__(
980+
self,
981+
timeout: float = 10.00,
982+
wsl_omc: str = 'omc',
983+
wsl_distribution: Optional[str] = None,
984+
wsl_user: Optional[str] = None,
985+
) -> None:
986+
987+
super().__init__(timeout=timeout)
988+
989+
# get wsl base command
990+
self._wsl_cmd = ['wsl']
991+
if isinstance(wsl_distribution, str):
992+
self._wsl_cmd += ['--distribution', wsl_distribution]
993+
if isinstance(wsl_user, str):
994+
self._wsl_cmd += ['--user', wsl_user]
995+
self._wsl_cmd += ['--']
996+
997+
# where to find OpenModelica
998+
self._wsl_omc = wsl_omc
999+
# start up omc executable, which is waiting for the ZMQ connection
1000+
self._omc_process = self._omc_process_get()
1001+
# connect to the running omc instance using ZMQ
1002+
self._omc_port = self._omc_port_get()
1003+
1004+
def _omc_process_get(self) -> subprocess.Popen:
1005+
my_env = os.environ.copy()
1006+
1007+
omc_command = self._wsl_cmd + [
1008+
self._wsl_omc,
1009+
"--locale=C",
1010+
"--interactive=zmq",
1011+
f"-z={self._random_string}"]
1012+
1013+
omc_process = subprocess.Popen(omc_command,
1014+
stdout=self._omc_loghandle,
1015+
stderr=self._omc_loghandle,
1016+
env=my_env)
1017+
return omc_process
1018+
1019+
def _omc_port_get(self) -> str:
1020+
omc_portfile_path: Optional[pathlib.Path] = None
1021+
port = None
1022+
1023+
# See if the omc server is running
1024+
attempts = 0
1025+
while True:
1026+
try:
1027+
omc_portfile_path = self._get_portfile_path()
1028+
if omc_portfile_path is not None:
1029+
output = subprocess.check_output(
1030+
args=self._wsl_cmd + ["cat", omc_portfile_path.as_posix()],
1031+
stderr=subprocess.DEVNULL,
1032+
)
1033+
port = output.decode().strip()
1034+
except subprocess.CalledProcessError:
1035+
pass
1036+
1037+
if port is not None:
1038+
break
1039+
1040+
attempts += 1
1041+
if attempts == 80.0:
1042+
raise OMCSessionException(f"WSL based OMC Server did not start (timeout={self._timeout}). "
1043+
f"Could not open port file {omc_portfile_path}. "
1044+
f"Log-file says:\n{self.get_log()}")
1045+
time.sleep(self._timeout / 80.0)
1046+
1047+
logger.info(f"WSL based OMC Server is up and running at ZMQ port {port} "
1048+
f"pid={self._omc_process.pid if isinstance(self._omc_process, subprocess.Popen) else '?'}")
1049+
1050+
return port

OMPython/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838

3939
from OMPython.ModelicaSystem import LinearizationResult, ModelicaSystem, ModelicaSystemCmd, ModelicaSystemError
4040
from OMPython.OMCSession import (OMCSessionCmd, OMCSessionException, OMCSessionZMQ,
41-
OMCProcessPort, OMCProcessLocal, OMCProcessDocker, OMCProcessDockerContainer)
41+
OMCProcessPort, OMCProcessLocal, OMCProcessDocker, OMCProcessDockerContainer,
42+
OMCProcessWSL)
4243

4344
# global names imported if import 'from OMPython import *' is used
4445
__all__ = [
@@ -54,4 +55,5 @@
5455
'OMCProcessLocal',
5556
'OMCProcessDocker',
5657
'OMCProcessDockerContainer',
58+
'OMCProcessWSL',
5759
]

0 commit comments

Comments
 (0)