Skip to content

Commit aec7777

Browse files
authored
Merge pull request #122 from calcut/fetch_all_messages_in_loop
Fetch all messages in loop, non-blocking mode
2 parents f2cb3bb + 8a65c4e commit aec7777

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

+22-3
Original file line numberDiff line numberDiff line change
@@ -834,14 +834,16 @@ def reconnect(self, resub_topics=True):
834834
feed = subscribed_topics.pop()
835835
self.subscribe(feed)
836836

837-
def loop(self, timeout=1):
837+
def loop(self, timeout=0):
838+
# pylint: disable = too-many-return-statements
838839
"""Non-blocking message loop. Use this method to
839840
check incoming subscription messages.
840841
Returns response codes of any messages received.
841842
842843
:param int timeout: Socket timeout, in seconds.
843844
844845
"""
846+
845847
if self._timestamp == 0:
846848
self._timestamp = time.monotonic()
847849
current_time = time.monotonic()
@@ -854,11 +856,28 @@ def loop(self, timeout=1):
854856
)
855857
rcs = self.ping()
856858
return rcs
859+
860+
stamp = time.monotonic()
857861
self._sock.settimeout(timeout)
858-
rc = self._wait_for_msg()
859-
return [rc] if rc else None
862+
rcs = []
863+
864+
while True:
865+
rc = self._wait_for_msg(timeout)
866+
if rc is None:
867+
break
868+
if time.monotonic() - stamp > self._recv_timeout:
869+
if self.logger is not None:
870+
self.logger.debug(
871+
f"Loop timed out, message queue not empty after {self._recv_timeout}s"
872+
)
873+
break
874+
rcs.append(rc)
875+
876+
return rcs if rcs else None
860877

861878
def _wait_for_msg(self, timeout=0.1):
879+
# pylint: disable = too-many-return-statements
880+
862881
"""Reads and processes network events."""
863882
# CPython socket module contains a timeout attribute
864883
if hasattr(self._socket_pool, "timeout"):

0 commit comments

Comments
 (0)