@@ -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).
@@ -846,17 +871,13 @@ def sendExpression(self, command: str, parsed: bool = True) -> Any:
846871
847872 logger .debug ("sendExpression(%r, parsed=%r)" , command , parsed )
848873
849- MAX_RETRIES = 50
850- attempts = 0
851- while attempts < MAX_RETRIES :
852- attempts += 1
853-
874+ loop = self ._timeout_loop (timestep = 0.05 )
875+ while next (loop ):
854876 try :
855877 self ._omc_zmq .send_string (str (command ), flags = zmq .NOBLOCK )
856878 break
857879 except zmq .error .Again :
858880 pass
859- time .sleep (self ._timeout / MAX_RETRIES )
860881 else :
861882 # in the deletion process, the content is cleared. Thus, any access to a class attribute must be checked
862883 try :
@@ -1111,11 +1132,8 @@ def _omc_port_get(self) -> str:
11111132 port = None
11121133
11131134 # See if the omc server is running
1114- MAX_RETRIES = 80
1115- attempts = 0
1116- while attempts < MAX_RETRIES :
1117- attempts += 1
1118-
1135+ loop = self ._timeout_loop (timestep = 0.1 )
1136+ while next (loop ):
11191137 omc_portfile_path = self ._get_portfile_path ()
11201138 if omc_portfile_path is not None and omc_portfile_path .is_file ():
11211139 # Read the port file
@@ -1124,7 +1142,6 @@ def _omc_port_get(self) -> str:
11241142 break
11251143 if port is not None :
11261144 break
1127- time .sleep (self ._timeout / MAX_RETRIES )
11281145 else :
11291146 logger .error (f"Docker did not start. Log-file says:\n { self .get_log ()} " )
11301147 raise OMCSessionException (f"OMC Server did not start (timeout={ self ._timeout } )." )
@@ -1209,11 +1226,8 @@ def _docker_process_get(self, docker_cid: str) -> Optional[DockerPopen]:
12091226 raise NotImplementedError ("Docker not supported on win32!" )
12101227
12111228 docker_process = None
1212- MAX_RETRIES = 40
1213- attempts = 0
1214- while attempts < MAX_RETRIES :
1215- attempts += 1
1216-
1229+ loop = self ._timeout_loop (timestep = 0.2 )
1230+ while next (loop ):
12171231 docker_top = subprocess .check_output (["docker" , "top" , docker_cid ]).decode ().strip ()
12181232 docker_process = None
12191233 for line in docker_top .split ("\n " ):
@@ -1226,7 +1240,6 @@ def _docker_process_get(self, docker_cid: str) -> Optional[DockerPopen]:
12261240 "is this a docker instance spawned without --pid=host?" ) from ex
12271241 if docker_process is not None :
12281242 break
1229- time .sleep (self ._timeout / MAX_RETRIES )
12301243 else :
12311244 logger .error (f"Docker did not start. Log-file says:\n { self .get_log ()} " )
12321245 raise OMCSessionException (f"Docker based OMC Server did not start (timeout={ self ._timeout } )." )
@@ -1251,11 +1264,8 @@ def _omc_port_get(self) -> str:
12511264 raise OMCSessionException (f"Invalid docker container ID: { self ._docker_container_id } " )
12521265
12531266 # See if the omc server is running
1254- MAX_RETRIES = 80
1255- attempts = 0
1256- while attempts < MAX_RETRIES :
1257- attempts += 1
1258-
1267+ loop = self ._timeout_loop (timestep = 0.1 )
1268+ while next (loop ):
12591269 omc_portfile_path = self ._get_portfile_path ()
12601270 if omc_portfile_path is not None :
12611271 try :
@@ -1268,7 +1278,6 @@ def _omc_port_get(self) -> str:
12681278 pass
12691279 if port is not None :
12701280 break
1271- time .sleep (self ._timeout / MAX_RETRIES )
12721281 else :
12731282 logger .error (f"Docker did not start. Log-file says:\n { self .get_log ()} " )
12741283 raise OMCSessionException (f"Docker based OMC Server did not start (timeout={ self ._timeout } )." )
@@ -1439,19 +1448,15 @@ def _docker_omc_start(self) -> Tuple[subprocess.Popen, DockerPopen, str]:
14391448 raise OMCSessionException (f"Invalid content for docker container ID file path: { docker_cid_file } " )
14401449
14411450 docker_cid = None
1442- MAX_RETRIES = 40
1443- attempts = 0
1444- while attempts < MAX_RETRIES :
1445- attempts += 1
1446-
1451+ loop = self ._timeout_loop (timestep = 0.1 )
1452+ while next (loop ):
14471453 try :
14481454 with open (file = docker_cid_file , mode = "r" , encoding = "utf-8" ) as fh :
14491455 docker_cid = fh .read ().strip ()
14501456 except IOError :
14511457 pass
14521458 if docker_cid is not None :
14531459 break
1454- time .sleep (self ._timeout / MAX_RETRIES )
14551460 else :
14561461 logger .error (f"Docker did not start. Log-file says:\n { self .get_log ()} " )
14571462 raise OMCSessionException (f"Docker did not start (timeout={ self ._timeout } might be too short "
@@ -1616,11 +1621,8 @@ def _omc_port_get(self) -> str:
16161621 port = None
16171622
16181623 # See if the omc server is running
1619- MAX_RETRIES = 80
1620- attempts = 0
1621- while attempts < MAX_RETRIES :
1622- attempts += 1
1623-
1624+ loop = self ._timeout_loop (timestep = 0.1 )
1625+ while next (loop ):
16241626 try :
16251627 omc_portfile_path = self ._get_portfile_path ()
16261628 if omc_portfile_path is not None :
@@ -1633,7 +1635,6 @@ def _omc_port_get(self) -> str:
16331635 pass
16341636 if port is not None :
16351637 break
1636- time .sleep (self ._timeout / MAX_RETRIES )
16371638 else :
16381639 logger .error (f"Docker did not start. Log-file says:\n { self .get_log ()} " )
16391640 raise OMCSessionException (f"WSL based OMC Server did not start (timeout={ self ._timeout } )." )
0 commit comments