Skip to content

Commit b7fe189

Browse files
committed
[OMCSession*] simplify code for timeout loops
1 parent 920d173 commit b7fe189

1 file changed

Lines changed: 37 additions & 36 deletions

File tree

OMPython/OMCSession.py

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -727,17 +727,13 @@ def sendExpression(self, command: str, parsed: bool = True) -> Any:
727727

728728
logger.debug("sendExpression(%r, parsed=%r)", command, parsed)
729729

730-
MAX_RETRIES = 50
731-
attempts = 0
732-
while attempts < MAX_RETRIES:
733-
attempts += 1
734-
730+
loop = self.omc_process._timeout_loop(timestep=0.05)
731+
while next(loop):
735732
try:
736733
self.omc_zmq.send_string(str(command), flags=zmq.NOBLOCK)
737734
break
738735
except zmq.error.Again:
739736
pass
740-
time.sleep(self._timeout / MAX_RETRIES)
741737
else:
742738
logger.error(f"Docker did not start. Log-file says:\n{self.omc_process.get_log()}")
743739
raise OMCSessionException(f"No connection with OMC (timeout={self._timeout}).")
@@ -912,6 +908,31 @@ def __del__(self):
912908
finally:
913909
self._omc_process = None
914910

911+
def _timeout_loop(
912+
self,
913+
timeout: Optional[float] = None,
914+
timestep: float = 0.1,
915+
):
916+
"""
917+
Helper (using yield) for while loops to check OMC startup / response. The loop is executed as long as True is
918+
returned, i.e. the first False will stop the while loop.
919+
"""
920+
921+
if timeout is None:
922+
timeout = self._timeout
923+
if timeout <= 0:
924+
raise OMCSessionException(f"Invalid timeout: {timeout}")
925+
926+
timer = 0
927+
yield True
928+
while True:
929+
timer += timestep
930+
if timer > timeout:
931+
break
932+
time.sleep(timestep)
933+
yield True
934+
yield False
935+
915936
def get_port(self) -> Optional[str]:
916937
"""
917938
Get the port to connect to the OMC process.
@@ -1033,11 +1054,8 @@ def _omc_port_get(self) -> str:
10331054
port = None
10341055

10351056
# See if the omc server is running
1036-
MAX_RETRIES = 80
1037-
attempts = 0
1038-
while attempts < MAX_RETRIES:
1039-
attempts += 1
1040-
1057+
loop = self._timeout_loop(timestep=0.1)
1058+
while next(loop):
10411059
omc_portfile_path = self._get_portfile_path()
10421060
if omc_portfile_path is not None and omc_portfile_path.is_file():
10431061
# Read the port file
@@ -1046,7 +1064,6 @@ def _omc_port_get(self) -> str:
10461064
break
10471065
if port is not None:
10481066
break
1049-
time.sleep(self._timeout / MAX_RETRIES)
10501067
else:
10511068
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
10521069
raise OMCSessionException(f"OMC Server did not start (timeout={self._timeout}).")
@@ -1131,11 +1148,8 @@ def _docker_process_get(self, docker_cid: str) -> Optional[DockerPopen]:
11311148
raise NotImplementedError("Docker not supported on win32!")
11321149

11331150
docker_process = None
1134-
MAX_RETRIES = 40
1135-
attempts = 0
1136-
while attempts < MAX_RETRIES:
1137-
attempts += 1
1138-
1151+
loop = self._timeout_loop(timestep=0.2)
1152+
while next(loop):
11391153
docker_top = subprocess.check_output(["docker", "top", docker_cid]).decode().strip()
11401154
docker_process = None
11411155
for line in docker_top.split("\n"):
@@ -1148,7 +1162,6 @@ def _docker_process_get(self, docker_cid: str) -> Optional[DockerPopen]:
11481162
"is this a docker instance spawned without --pid=host?") from ex
11491163
if docker_process is not None:
11501164
break
1151-
time.sleep(self._timeout / MAX_RETRIES)
11521165
else:
11531166
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
11541167
raise OMCSessionException(f"Docker based OMC Server did not start (timeout={self._timeout}).")
@@ -1173,11 +1186,8 @@ def _omc_port_get(self) -> str:
11731186
raise OMCSessionException(f"Invalid docker container ID: {self._docker_container_id}")
11741187

11751188
# See if the omc server is running
1176-
MAX_RETRIES = 80
1177-
attempts = 0
1178-
while attempts < MAX_RETRIES:
1179-
attempts += 1
1180-
1189+
loop = self._timeout_loop(timestep=0.1)
1190+
while next(loop):
11811191
omc_portfile_path = self._get_portfile_path()
11821192
if omc_portfile_path is not None:
11831193
try:
@@ -1190,7 +1200,6 @@ def _omc_port_get(self) -> str:
11901200
pass
11911201
if port is not None:
11921202
break
1193-
time.sleep(self._timeout / MAX_RETRIES)
11941203
else:
11951204
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
11961205
raise OMCSessionException(f"Docker based OMC Server did not start (timeout={self._timeout}).")
@@ -1361,19 +1370,15 @@ def _docker_omc_start(self) -> Tuple[subprocess.Popen, DockerPopen, str]:
13611370
raise OMCSessionException(f"Invalid content for docker container ID file path: {docker_cid_file}")
13621371

13631372
docker_cid = None
1364-
MAX_RETRIES = 40
1365-
attempts = 0
1366-
while attempts < MAX_RETRIES:
1367-
attempts += 1
1368-
1373+
loop = self._timeout_loop(timestep=0.1)
1374+
while next(loop):
13691375
try:
13701376
with open(file=docker_cid_file, mode="r", encoding="utf-8") as fh:
13711377
docker_cid = fh.read().strip()
13721378
except IOError:
13731379
pass
13741380
if docker_cid is not None:
13751381
break
1376-
time.sleep(self._timeout / MAX_RETRIES)
13771382
else:
13781383
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
13791384
raise OMCSessionException(f"Docker did not start (timeout={self._timeout} might be too short "
@@ -1538,11 +1543,8 @@ def _omc_port_get(self) -> str:
15381543
port = None
15391544

15401545
# See if the omc server is running
1541-
MAX_RETRIES = 80
1542-
attempts = 0
1543-
while attempts < MAX_RETRIES:
1544-
attempts += 1
1545-
1546+
loop = self._timeout_loop(timestep=0.1)
1547+
while next(loop):
15461548
try:
15471549
omc_portfile_path = self._get_portfile_path()
15481550
if omc_portfile_path is not None:
@@ -1555,7 +1557,6 @@ def _omc_port_get(self) -> str:
15551557
pass
15561558
if port is not None:
15571559
break
1558-
time.sleep(self._timeout / MAX_RETRIES)
15591560
else:
15601561
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
15611562
raise OMCSessionException(f"WSL based OMC Server did not start (timeout={self._timeout}).")

0 commit comments

Comments
 (0)