Skip to content

Commit

Permalink
use selectors instead of select.select (#16)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
skshetry authored Jun 9, 2020
1 parent bc14685 commit b007288
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
13 changes: 10 additions & 3 deletions mockssh/server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import errno
import logging
import os
import select
import socket
import subprocess
import threading
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
paramiko
selectors2; python_version < '3.4'

0 comments on commit b007288

Please sign in to comment.