From b007288baf53283b54be2ef3512a872c25e6e869 Mon Sep 17 00:00:00 2001 From: Saugat Pachhai Date: Tue, 9 Jun 2020 08:26:45 +0000 Subject: [PATCH] use selectors instead of select.select (#16) * use selectors instead of select.select select.select() cannot work with fd > FDSET_SIZE == 1024 `selectors` uses most efficient implementation of `*poll()` wherever supported, otherwise fallbacks to `select.select` eg. in Windows. * register socket before loop --- mockssh/server.py | 13 ++++++++++--- requirements.txt | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/mockssh/server.py b/mockssh/server.py index 2b69ffe..16d04aa 100644 --- a/mockssh/server.py +++ b/mockssh/server.py @@ -1,7 +1,6 @@ import errno import logging import os -import select import socket import subprocess import threading @@ -11,6 +10,12 @@ except ImportError: # Python 2.7 from Queue import Queue +try: + import selectors +except ImportError: # Python 2.7 + import selectors2 as selectors + + import paramiko from mockssh import sftp @@ -129,10 +134,12 @@ def __enter__(self): def _run(self): sock = self._socket + selector = selectors.DefaultSelector() + selector.register(sock, selectors.EVENT_READ) while sock.fileno() > 0: self.log.debug("Waiting for incoming connections ...") - rlist, _, _ = select.select([sock], [], [], 1.0) - if rlist: + events = selector.select(timeout=1.0) + if events: try: conn, addr = sock.accept() except OSError as ex: diff --git a/requirements.txt b/requirements.txt index 8608c1b..487233b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ paramiko +selectors2; python_version < '3.4' \ No newline at end of file