Skip to content

Commit 6e3ed77

Browse files
authored
Merge pull request #169 from vladak/loop_vs_timeout
loop() timeout parameter should be absolute
2 parents 7b63446 + 4f57b20 commit 6e3ed77

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

+9-16
Original file line numberDiff line numberDiff line change
@@ -980,11 +980,10 @@ def reconnect(self, resub_topics: bool = True) -> int:
980980

981981
def loop(self, timeout: float = 0) -> Optional[list[int]]:
982982
# 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.
986985
987-
:param float timeout: Socket timeout, in seconds.
986+
:param float timeout: return after this timeout, in seconds.
988987
989988
"""
990989

@@ -1002,23 +1001,19 @@ def loop(self, timeout: float = 0) -> Optional[list[int]]:
10021001
return rcs
10031002

10041003
stamp = time.monotonic()
1005-
self._sock.settimeout(timeout)
10061004
rcs = []
10071005

10081006
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")
10161012
break
1017-
rcs.append(rc)
10181013

10191014
return rcs if rcs else None
10201015

1021-
def _wait_for_msg(self, timeout: float = 0.1) -> Optional[int]:
1016+
def _wait_for_msg(self) -> Optional[int]:
10221017
# pylint: disable = too-many-return-statements
10231018

10241019
"""Reads and processes network events.
@@ -1039,8 +1034,6 @@ def _wait_for_msg(self, timeout: float = 0.1) -> Optional[int]:
10391034
return None
10401035
raise MMQTTException from error
10411036

1042-
# Block while we parse the rest of the response
1043-
self._sock.settimeout(timeout)
10441037
if res in [None, b"", b"\x00"]:
10451038
# If we get here, it means that there is nothing to be received
10461039
return None

examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,4 @@ def message(client, topic, message):
106106
print(f"Sending photocell value: {photocell_val}")
107107
mqtt_client.publish(default_topic, photocell_val)
108108
photocell_val += 1
109-
time.sleep(0.5)
109+
time.sleep(3)

0 commit comments

Comments
 (0)