Skip to content

Commit ada825e

Browse files
committed
[OMCProcessWSL] (untested) WSL based OMPython with OMC via ZMQ
1 parent f9bfcf6 commit ada825e

2 files changed

Lines changed: 81 additions & 1 deletion

File tree

OMPython/OMCSession.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,3 +972,81 @@ 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+
omhome: Optional[str] = None,
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._omhome = omhome
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_path = 'omc'
1008+
if self._omhome is not None:
1009+
omc_path = f"{self._omhome}/{omc_path}"
1010+
1011+
omc_command = self._wsl_cmd + [
1012+
omc_path,
1013+
"--locale=C",
1014+
"--interactive=zmq",
1015+
f"-z={self._random_string}"]
1016+
1017+
omc_process = subprocess.Popen(omc_command,
1018+
stdout=self._omc_loghandle,
1019+
stderr=self._omc_loghandle,
1020+
env=my_env)
1021+
return omc_process
1022+
1023+
def _omc_port_get(self) -> str:
1024+
omc_portfile_path: Optional[pathlib.Path] = None
1025+
port = None
1026+
1027+
# See if the omc server is running
1028+
attempts = 0
1029+
while True:
1030+
try:
1031+
omc_portfile_path = self._get_portfile_path()
1032+
if omc_portfile_path is not None:
1033+
output = subprocess.check_output(args=self._wsl_cmd + ["cat", omc_portfile_path.as_posix()],
1034+
stderr=subprocess.DEVNULL)
1035+
port = output.decode().strip()
1036+
except subprocess.CalledProcessError:
1037+
pass
1038+
1039+
if port is not None:
1040+
break
1041+
1042+
attempts += 1
1043+
if attempts == 80.0:
1044+
raise OMCSessionException(f"WSL based OMC Server did not start (timeout={self._timeout}). "
1045+
f"Could not open port file {omc_portfile_path}. "
1046+
f"Log-file says:\n{self.get_log()}")
1047+
time.sleep(self._timeout / 80.0)
1048+
1049+
logger.info(f"WSL based OMC Server is up and running at ZMQ port {port} "
1050+
f"pid={self._omc_process.pid if isinstance(self._omc_process, subprocess.Popen) else '?'}")
1051+
1052+
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)