@@ -980,11 +980,10 @@ def reconnect(self, resub_topics: bool = True) -> int:
980
980
981
981
def loop (self , timeout : float = 0 ) -> Optional [list [int ]]:
982
982
# pylint: disable = too-many-return-statements
983
- """Non-blocking message loop. Use this method to
984
- check incoming subscription messages.
985
- Returns response codes of any messages received.
983
+ """Non-blocking message loop. Use this method to check for incoming messages.
984
+ Returns list of response codes of any messages received or None.
986
985
987
- :param float timeout: Socket timeout, in seconds.
986
+ :param float timeout: return after this timeout, in seconds.
988
987
989
988
"""
990
989
@@ -1002,23 +1001,19 @@ def loop(self, timeout: float = 0) -> Optional[list[int]]:
1002
1001
return rcs
1003
1002
1004
1003
stamp = time .monotonic ()
1005
- self ._sock .settimeout (timeout )
1006
1004
rcs = []
1007
1005
1008
1006
while True :
1009
- rc = self ._wait_for_msg (timeout )
1010
- if rc is None :
1011
- break
1012
- if time .monotonic () - stamp > self ._recv_timeout :
1013
- self .logger .debug (
1014
- f"Loop timed out, message queue not empty after { self ._recv_timeout } s"
1015
- )
1007
+ rc = self ._wait_for_msg ()
1008
+ if rc is not None :
1009
+ rcs .append (rc )
1010
+ if time .monotonic () - stamp > timeout :
1011
+ self .logger .debug (f"Loop timed out after { timeout } seconds" )
1016
1012
break
1017
- rcs .append (rc )
1018
1013
1019
1014
return rcs if rcs else None
1020
1015
1021
- def _wait_for_msg (self , timeout : float = 0.1 ) -> Optional [int ]:
1016
+ def _wait_for_msg (self ) -> Optional [int ]:
1022
1017
# pylint: disable = too-many-return-statements
1023
1018
1024
1019
"""Reads and processes network events.
@@ -1039,8 +1034,6 @@ def _wait_for_msg(self, timeout: float = 0.1) -> Optional[int]:
1039
1034
return None
1040
1035
raise MMQTTException from error
1041
1036
1042
- # Block while we parse the rest of the response
1043
- self ._sock .settimeout (timeout )
1044
1037
if res in [None , b"" , b"\x00 " ]:
1045
1038
# If we get here, it means that there is nothing to be received
1046
1039
return None
0 commit comments