Skip to content

Commit 5cb6608

Browse files
committed
[OMCProcessWSL] (untested) WSL based OMPython with OMC via ZMQ
1 parent 954b40d commit 5cb6608

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

OMPython/OMCSession.py

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

972972
return omc_process, docker_process
973+
974+
975+
class OMCProcessWSL(OMCProcess):
976+
977+
def __init__(
978+
self,
979+
timeout: float = 10.00,
980+
omhome: Optional[str] = None,
981+
wsl_distribution: Optional[str] = None,
982+
wsl_user: Optional[str] = None,
983+
) -> None:
984+
985+
super().__init__(timeout=timeout)
986+
987+
# get wsl base command
988+
self._wsl_cmd = ['wsl']
989+
if isinstance(wsl_distribution, str):
990+
self._wsl_cmd += ['--distribution', wsl_distribution]
991+
if isinstance(wsl_user, str):
992+
self._wsl_cmd += ['--user', wsl_user]
993+
self._wsl_cmd += ['--']
994+
995+
# where to find OpenModelica
996+
self._omhome = omhome
997+
# start up omc executable, which is waiting for the ZMQ connection
998+
self._omc_process = self._omc_process_get()
999+
# connect to the running omc instance using ZMQ
1000+
self._omc_port = self._omc_port_get()
1001+
1002+
def _omc_process_get(self) -> subprocess.Popen:
1003+
my_env = os.environ.copy()
1004+
1005+
omc_path = 'omc'
1006+
if self._omhome is not None:
1007+
omc_path = f"{self._omhome}/{omc_path}"
1008+
1009+
omc_command = self._wsl_cmd + [
1010+
omc_path,
1011+
"--locale=C",
1012+
"--interactive=zmq",
1013+
f"-z={self._random_string}"]
1014+
1015+
omc_process = subprocess.Popen(omc_command,
1016+
stdout=self._omc_loghandle,
1017+
stderr=self._omc_loghandle,
1018+
env=my_env)
1019+
return omc_process
1020+
1021+
def _omc_port_get(self) -> str:
1022+
omc_portfile_path: Optional[pathlib.Path] = None
1023+
port = None
1024+
1025+
# See if the omc server is running
1026+
attempts = 0
1027+
while True:
1028+
try:
1029+
omc_portfile_path = self._get_portfile_path()
1030+
if omc_portfile_path is not None:
1031+
output = subprocess.check_output(args=self._wsl_cmd + ["cat", omc_portfile_path.as_posix()],
1032+
stderr=subprocess.DEVNULL)
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)