Skip to content

Commit fbfb6ec

Browse files
author
brentru
committed
CircuitPython lacks select() module, use socket timeouttype instead of non-blocking SSL socket to avoid SSLWantWrite/SSLWantRead errors, https://docs.python.org/3/library/ssl.html#notes-on-non-blocking-sockets
1 parent 4af0ad0 commit fbfb6ec

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ def _get_socket(self, host, port, *, timeout=1):
273273

274274
connect_host = addr_info[-1][0]
275275
if port == 8883:
276-
sock = self._ssl_context.wrap_socket(sock, server_hostname=host)
276+
sock = self._ssl_context.wrap_socket(sock,
277+
server_hostname=host)
277278
connect_host = host
278279
sock.settimeout(timeout)
279280

@@ -796,9 +797,11 @@ def reconnect(self, resub_topics=True):
796797
feed = subscribed_topics.pop()
797798
self.subscribe(feed)
798799

799-
def loop(self):
800+
def loop(self, timeout=0.01):
800801
"""Non-blocking message loop. Use this method to
801802
check incoming subscription messages.
803+
:param float timeout: Set timeout in seconds for
804+
polling the message queue.
802805
"""
803806
if self._timestamp == 0:
804807
self._timestamp = time.monotonic()
@@ -812,17 +815,20 @@ def loop(self):
812815
)
813816
self.ping()
814817
self._timestamp = 0
815-
return self._wait_for_msg()
818+
return self._wait_for_msg(timeout)
816819

817-
def _wait_for_msg(self):
820+
def _wait_for_msg(self, timeout=0.01):
818821
"""Reads and processes network events."""
819822
res = bytearray(1) #TODO: This should be a globally shared buffer for readinto
820823

821-
self._sock.setblocking(False)
824+
self._sock.settimeout(timeout)
822825
try:
823826
self._sock.recv_into(res, 1)
824827
except BlockingIOError: # fix for macOS Errno
825828
return None
829+
except self._socket_pool.timeout:
830+
return None
831+
826832
self._sock.setblocking(True)
827833
if res in [None, b""]:
828834
return None

0 commit comments

Comments
 (0)