@@ -724,6 +724,31 @@ def __del__(self):
724724 finally :
725725 self ._omc_process = None
726726
727+ def _timeout_loop (
728+ self ,
729+ timeout : Optional [float ] = None ,
730+ timestep : float = 0.1 ,
731+ ):
732+ """
733+ Helper (using yield) for while loops to check OMC startup / response. The loop is executed as long as True is
734+ returned, i.e. the first False will stop the while loop.
735+ """
736+
737+ if timeout is None :
738+ timeout = self ._timeout
739+ if timeout <= 0 :
740+ raise OMCSessionException (f"Invalid timeout: { timeout } " )
741+
742+ timer = 0.0
743+ yield True
744+ while True :
745+ timer += timestep
746+ if timer > timeout :
747+ break
748+ time .sleep (timestep )
749+ yield True
750+ yield False
751+
727752 def set_timeout (self , timeout : Optional [float ] = None ) -> float :
728753 """
729754 Set the timeout to be used for OMC communication (OMCSession).
@@ -844,17 +869,13 @@ def sendExpression(self, command: str, parsed: bool = True) -> Any:
844869
845870 logger .debug ("sendExpression(%r, parsed=%r)" , command , parsed )
846871
847- MAX_RETRIES = 50
848- attempts = 0
849- while attempts < MAX_RETRIES :
850- attempts += 1
851-
872+ loop = self ._timeout_loop (timestep = 0.05 )
873+ while next (loop ):
852874 try :
853875 self ._omc_zmq .send_string (str (command ), flags = zmq .NOBLOCK )
854876 break
855877 except zmq .error .Again :
856878 pass
857- time .sleep (self ._timeout / MAX_RETRIES )
858879 else :
859880 # in the deletion process, the content is cleared. Thus, any access to a class attribute must be checked
860881 try :
@@ -1093,11 +1114,8 @@ def _omc_port_get(self) -> str:
10931114 port = None
10941115
10951116 # See if the omc server is running
1096- MAX_RETRIES = 80
1097- attempts = 0
1098- while attempts < MAX_RETRIES :
1099- attempts += 1
1100-
1117+ loop = self ._timeout_loop (timestep = 0.1 )
1118+ while next (loop ):
11011119 omc_portfile_path = self ._get_portfile_path ()
11021120 if omc_portfile_path is not None and omc_portfile_path .is_file ():
11031121 # Read the port file
@@ -1106,7 +1124,6 @@ def _omc_port_get(self) -> str:
11061124 break
11071125 if port is not None :
11081126 break
1109- time .sleep (self ._timeout / MAX_RETRIES )
11101127 else :
11111128 logger .error (f"Docker did not start. Log-file says:\n { self .get_log ()} " )
11121129 raise OMCSessionException (f"OMC Server did not start (timeout={ self ._timeout } )." )
@@ -1191,11 +1208,8 @@ def _docker_process_get(self, docker_cid: str) -> Optional[DockerPopen]:
11911208 raise NotImplementedError ("Docker not supported on win32!" )
11921209
11931210 docker_process = None
1194- MAX_RETRIES = 40
1195- attempts = 0
1196- while attempts < MAX_RETRIES :
1197- attempts += 1
1198-
1211+ loop = self ._timeout_loop (timestep = 0.2 )
1212+ while next (loop ):
11991213 docker_top = subprocess .check_output (["docker" , "top" , docker_cid ]).decode ().strip ()
12001214 docker_process = None
12011215 for line in docker_top .split ("\n " ):
@@ -1208,7 +1222,6 @@ def _docker_process_get(self, docker_cid: str) -> Optional[DockerPopen]:
12081222 "is this a docker instance spawned without --pid=host?" ) from ex
12091223 if docker_process is not None :
12101224 break
1211- time .sleep (self ._timeout / MAX_RETRIES )
12121225 else :
12131226 logger .error (f"Docker did not start. Log-file says:\n { self .get_log ()} " )
12141227 raise OMCSessionException (f"Docker based OMC Server did not start (timeout={ self ._timeout } )." )
@@ -1233,11 +1246,8 @@ def _omc_port_get(self) -> str:
12331246 raise OMCSessionException (f"Invalid docker container ID: { self ._docker_container_id } " )
12341247
12351248 # See if the omc server is running
1236- MAX_RETRIES = 80
1237- attempts = 0
1238- while attempts < MAX_RETRIES :
1239- attempts += 1
1240-
1249+ loop = self ._timeout_loop (timestep = 0.1 )
1250+ while next (loop ):
12411251 omc_portfile_path = self ._get_portfile_path ()
12421252 if omc_portfile_path is not None :
12431253 try :
@@ -1250,7 +1260,6 @@ def _omc_port_get(self) -> str:
12501260 pass
12511261 if port is not None :
12521262 break
1253- time .sleep (self ._timeout / MAX_RETRIES )
12541263 else :
12551264 logger .error (f"Docker did not start. Log-file says:\n { self .get_log ()} " )
12561265 raise OMCSessionException (f"Docker based OMC Server did not start (timeout={ self ._timeout } )." )
@@ -1421,19 +1430,15 @@ def _docker_omc_start(self) -> Tuple[subprocess.Popen, DockerPopen, str]:
14211430 raise OMCSessionException (f"Invalid content for docker container ID file path: { docker_cid_file } " )
14221431
14231432 docker_cid = None
1424- MAX_RETRIES = 40
1425- attempts = 0
1426- while attempts < MAX_RETRIES :
1427- attempts += 1
1428-
1433+ loop = self ._timeout_loop (timestep = 0.1 )
1434+ while next (loop ):
14291435 try :
14301436 with open (file = docker_cid_file , mode = "r" , encoding = "utf-8" ) as fh :
14311437 docker_cid = fh .read ().strip ()
14321438 except IOError :
14331439 pass
14341440 if docker_cid is not None :
14351441 break
1436- time .sleep (self ._timeout / MAX_RETRIES )
14371442 else :
14381443 logger .error (f"Docker did not start. Log-file says:\n { self .get_log ()} " )
14391444 raise OMCSessionException (f"Docker did not start (timeout={ self ._timeout } might be too short "
@@ -1598,11 +1603,8 @@ def _omc_port_get(self) -> str:
15981603 port = None
15991604
16001605 # See if the omc server is running
1601- MAX_RETRIES = 80
1602- attempts = 0
1603- while attempts < MAX_RETRIES :
1604- attempts += 1
1605-
1606+ loop = self ._timeout_loop (timestep = 0.1 )
1607+ while next (loop ):
16061608 try :
16071609 omc_portfile_path = self ._get_portfile_path ()
16081610 if omc_portfile_path is not None :
@@ -1615,7 +1617,6 @@ def _omc_port_get(self) -> str:
16151617 pass
16161618 if port is not None :
16171619 break
1618- time .sleep (self ._timeout / MAX_RETRIES )
16191620 else :
16201621 logger .error (f"Docker did not start. Log-file says:\n { self .get_log ()} " )
16211622 raise OMCSessionException (f"WSL based OMC Server did not start (timeout={ self ._timeout } )." )
0 commit comments