diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 7f96ea9d..6b974ddc 100644 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -980,11 +980,10 @@ def reconnect(self, resub_topics: bool = True) -> int: def loop(self, timeout: float = 0) -> Optional[list[int]]: # pylint: disable = too-many-return-statements - """Non-blocking message loop. Use this method to - check incoming subscription messages. - Returns response codes of any messages received. + """Non-blocking message loop. Use this method to check for incoming messages. + Returns list of response codes of any messages received or None. - :param float timeout: Socket timeout, in seconds. + :param float timeout: return after this timeout, in seconds. """ @@ -1002,23 +1001,19 @@ def loop(self, timeout: float = 0) -> Optional[list[int]]: return rcs stamp = time.monotonic() - self._sock.settimeout(timeout) rcs = [] while True: - rc = self._wait_for_msg(timeout) - if rc is None: - break - if time.monotonic() - stamp > self._recv_timeout: - self.logger.debug( - f"Loop timed out, message queue not empty after {self._recv_timeout}s" - ) + rc = self._wait_for_msg() + if rc is not None: + rcs.append(rc) + if time.monotonic() - stamp > timeout: + self.logger.debug(f"Loop timed out after {timeout} seconds") break - rcs.append(rc) return rcs if rcs else None - def _wait_for_msg(self, timeout: float = 0.1) -> Optional[int]: + def _wait_for_msg(self) -> Optional[int]: # pylint: disable = too-many-return-statements """Reads and processes network events. @@ -1039,8 +1034,6 @@ def _wait_for_msg(self, timeout: float = 0.1) -> Optional[int]: return None raise MMQTTException from error - # Block while we parse the rest of the response - self._sock.settimeout(timeout) if res in [None, b"", b"\x00"]: # If we get here, it means that there is nothing to be received return None diff --git a/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py b/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py index 8a5ce8ba..30611384 100644 --- a/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py +++ b/examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py @@ -109,4 +109,4 @@ def message(client, topic, message): print("Sending photocell value: %d" % photocell_val) mqtt_client.publish(default_topic, photocell_val) photocell_val += 1 - time.sleep(0.5) + time.sleep(3)