diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 2a5889fd..743fdbc4 100644 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -834,7 +834,8 @@ def reconnect(self, resub_topics=True): feed = subscribed_topics.pop() self.subscribe(feed) - def loop(self, timeout=1): + def loop(self, timeout=0): + # 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. @@ -842,6 +843,7 @@ def loop(self, timeout=1): :param int timeout: Socket timeout, in seconds. """ + if self._timestamp == 0: self._timestamp = time.monotonic() current_time = time.monotonic() @@ -854,11 +856,28 @@ def loop(self, timeout=1): ) rcs = self.ping() return rcs + + stamp = time.monotonic() self._sock.settimeout(timeout) - rc = self._wait_for_msg() - return [rc] if rc else None + rcs = [] + + while True: + rc = self._wait_for_msg(timeout) + if rc is None: + break + if time.monotonic() - stamp > self._recv_timeout: + if self.logger is not None: + self.logger.debug( + f"Loop timed out, message queue not empty after {self._recv_timeout}s" + ) + break + rcs.append(rc) + + return rcs if rcs else None def _wait_for_msg(self, timeout=0.1): + # pylint: disable = too-many-return-statements + """Reads and processes network events.""" # CPython socket module contains a timeout attribute if hasattr(self._socket_pool, "timeout"):