-
Notifications
You must be signed in to change notification settings - Fork 738
Use Python selectors instead of select #697
Copy link
Copy link
Open
Labels
Status: AvailableNo one has claimed responsibility for resolving this issue.No one has claimed responsibility for resolving this issue.Type: EnhancementA new feature for a minor or major release.A new feature for a minor or major release.
Description
Using Python select.select(..) method limits the open connections to 340:
- limit of 1024 connections #183
- Unable to make 340 connections #238
- Question about mqtt and threading #499
- mqtt not open thread 340 #662
Python select docs encourages users to use selectors instead of select.
Would it be possible to change the select usage to use selectors? The following implementation worked for us:
import selectors
class Client:
[...]
def _loop(self, timeout: float = 1.0) -> int:
if timeout < 0.0:
raise ValueError("Invalid timeout.")
sel = selectors.DefaultSelector()
eventmask = selectors.EVENT_READ
with suppress(IndexError):
packet = self._out_packet.popleft()
self._out_packet.appendleft(packet)
eventmask = selectors.EVENT_WRITE | eventmask
if self._sockpairR is None:
sel.register(self._sock, eventmask)
else:
sel.register(self._sock, eventmask)
sel.register(self._sockpairR, selectors.EVENT_READ)
pending_bytes = 0
if hasattr(self._sock, "pending"):
pending_bytes = self._sock.pending()
if pending_bytes > 0:
timeout = 0.0
try:
events = sel.select(timeout)
except TypeError:
return int(MQTT_ERR_CONN_LOST)
except ValueError:
return int(MQTT_ERR_CONN_LOST)
except Exception:
return int(MQTT_ERR_UNKNOWN)
socklist: list[list] = [[], []]
for key, _event in events:
if key.events & selectors.EVENT_READ:
socklist[0].append(key.fileobj)
if key.events & selectors.EVENT_WRITE:
socklist[1].append(key.fileobj)
if self._sock in socklist[0] or pending_bytes > 0:
rc = self.loop_read()
if rc or self._sock is None:
return int(rc)
if self._sockpairR and self._sockpairR in socklist[0]:
socklist[1].insert(0, self._sock)
with suppress(BlockingIOError):
self._sockpairR.recv(10000)
if self._sock in socklist[1]:
rc = self.loop_write()
if rc or self._sock is None:
return int(rc)
sel.close()
return int(self.loop_misc())Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Status: AvailableNo one has claimed responsibility for resolving this issue.No one has claimed responsibility for resolving this issue.Type: EnhancementA new feature for a minor or major release.A new feature for a minor or major release.