@@ -753,6 +753,31 @@ def __del__(self):
753753 finally :
754754 self ._omc_process = None
755755
756+ def _timeout_loop (
757+ self ,
758+ timeout : Optional [float ] = None ,
759+ timestep : float = 0.1 ,
760+ ):
761+ """
762+ Helper (using yield) for while loops to check OMC startup / response. The loop is executed as long as True is
763+ returned, i.e. the first False will stop the while loop.
764+ """
765+
766+ if timeout is None :
767+ timeout = self ._timeout
768+ if timeout <= 0 :
769+ raise OMCSessionException (f"Invalid timeout: { timeout } " )
770+
771+ timer = 0.0
772+ yield True
773+ while True :
774+ timer += timestep
775+ if timer > timeout :
776+ break
777+ time .sleep (timestep )
778+ yield True
779+ yield False
780+
756781 def set_timeout (self , timeout : Optional [float ] = None ) -> float :
757782 """
758783 Set the timeout to be used for OMC communication (OMCSession).
@@ -873,17 +898,13 @@ def sendExpression(self, command: str, parsed: bool = True) -> Any:
873898
874899 logger .debug ("sendExpression(%r, parsed=%r)" , command , parsed )
875900
876- MAX_RETRIES = 50
877- attempts = 0
878- while attempts < MAX_RETRIES :
879- attempts += 1
880-
901+ loop = self ._timeout_loop (timestep = 0.05 )
902+ while next (loop ):
881903 try :
882904 self ._omc_zmq .send_string (str (command ), flags = zmq .NOBLOCK )
883905 break
884906 except zmq .error .Again :
885907 pass
886- time .sleep (self ._timeout / MAX_RETRIES )
887908 else :
888909 # in the deletion process, the content is cleared. Thus, any access to a class attribute must be checked
889910 try :
@@ -1122,11 +1143,8 @@ def _omc_port_get(self) -> str:
11221143 port = None
11231144
11241145 # See if the omc server is running
1125- MAX_RETRIES = 80
1126- attempts = 0
1127- while attempts < MAX_RETRIES :
1128- attempts += 1
1129-
1146+ loop = self ._timeout_loop (timestep = 0.1 )
1147+ while next (loop ):
11301148 omc_portfile_path = self ._get_portfile_path ()
11311149 if omc_portfile_path is not None and omc_portfile_path .is_file ():
11321150 # Read the port file
@@ -1135,7 +1153,6 @@ def _omc_port_get(self) -> str:
11351153 break
11361154 if port is not None :
11371155 break
1138- time .sleep (self ._timeout / MAX_RETRIES )
11391156 else :
11401157 logger .error (f"Docker did not start. Log-file says:\n { self .get_log ()} " )
11411158 raise OMCSessionException (f"OMC Server did not start (timeout={ self ._timeout } )." )
@@ -1220,11 +1237,8 @@ def _docker_process_get(self, docker_cid: str) -> Optional[DockerPopen]:
12201237 raise NotImplementedError ("Docker not supported on win32!" )
12211238
12221239 docker_process = None
1223- MAX_RETRIES = 40
1224- attempts = 0
1225- while attempts < MAX_RETRIES :
1226- attempts += 1
1227-
1240+ loop = self ._timeout_loop (timestep = 0.2 )
1241+ while next (loop ):
12281242 docker_top = subprocess .check_output (["docker" , "top" , docker_cid ]).decode ().strip ()
12291243 docker_process = None
12301244 for line in docker_top .split ("\n " ):
@@ -1237,7 +1251,6 @@ def _docker_process_get(self, docker_cid: str) -> Optional[DockerPopen]:
12371251 "is this a docker instance spawned without --pid=host?" ) from ex
12381252 if docker_process is not None :
12391253 break
1240- time .sleep (self ._timeout / MAX_RETRIES )
12411254 else :
12421255 logger .error (f"Docker did not start. Log-file says:\n { self .get_log ()} " )
12431256 raise OMCSessionException (f"Docker based OMC Server did not start (timeout={ self ._timeout } )." )
@@ -1262,11 +1275,8 @@ def _omc_port_get(self) -> str:
12621275 raise OMCSessionException (f"Invalid docker container ID: { self ._docker_container_id } " )
12631276
12641277 # See if the omc server is running
1265- MAX_RETRIES = 80
1266- attempts = 0
1267- while attempts < MAX_RETRIES :
1268- attempts += 1
1269-
1278+ loop = self ._timeout_loop (timestep = 0.1 )
1279+ while next (loop ):
12701280 omc_portfile_path = self ._get_portfile_path ()
12711281 if omc_portfile_path is not None :
12721282 try :
@@ -1279,7 +1289,6 @@ def _omc_port_get(self) -> str:
12791289 pass
12801290 if port is not None :
12811291 break
1282- time .sleep (self ._timeout / MAX_RETRIES )
12831292 else :
12841293 logger .error (f"Docker did not start. Log-file says:\n { self .get_log ()} " )
12851294 raise OMCSessionException (f"Docker based OMC Server did not start (timeout={ self ._timeout } )." )
@@ -1450,19 +1459,15 @@ def _docker_omc_start(self) -> Tuple[subprocess.Popen, DockerPopen, str]:
14501459 raise OMCSessionException (f"Invalid content for docker container ID file path: { docker_cid_file } " )
14511460
14521461 docker_cid = None
1453- MAX_RETRIES = 40
1454- attempts = 0
1455- while attempts < MAX_RETRIES :
1456- attempts += 1
1457-
1462+ loop = self ._timeout_loop (timestep = 0.1 )
1463+ while next (loop ):
14581464 try :
14591465 with open (file = docker_cid_file , mode = "r" , encoding = "utf-8" ) as fh :
14601466 docker_cid = fh .read ().strip ()
14611467 except IOError :
14621468 pass
14631469 if docker_cid is not None :
14641470 break
1465- time .sleep (self ._timeout / MAX_RETRIES )
14661471 else :
14671472 logger .error (f"Docker did not start. Log-file says:\n { self .get_log ()} " )
14681473 raise OMCSessionException (f"Docker did not start (timeout={ self ._timeout } might be too short "
@@ -1627,11 +1632,8 @@ def _omc_port_get(self) -> str:
16271632 port = None
16281633
16291634 # See if the omc server is running
1630- MAX_RETRIES = 80
1631- attempts = 0
1632- while attempts < MAX_RETRIES :
1633- attempts += 1
1634-
1635+ loop = self ._timeout_loop (timestep = 0.1 )
1636+ while next (loop ):
16351637 try :
16361638 omc_portfile_path = self ._get_portfile_path ()
16371639 if omc_portfile_path is not None :
@@ -1644,7 +1646,6 @@ def _omc_port_get(self) -> str:
16441646 pass
16451647 if port is not None :
16461648 break
1647- time .sleep (self ._timeout / MAX_RETRIES )
16481649 else :
16491650 logger .error (f"Docker did not start. Log-file says:\n { self .get_log ()} " )
16501651 raise OMCSessionException (f"WSL based OMC Server did not start (timeout={ self ._timeout } )." )
0 commit comments