selectors2

raw JSON →
2.0.2 verified Fri May 01 auth: no python maintenance

Back-ported, durable, and portable selectors module for Python. Current version 2.0.2. Maintained but stable; rarely updated.

pip install selectors2
error AttributeError: module 'selectors' has no attribute 'DefaultSelector'
cause Importing from stdlib 'selectors' instead of 'selectors2'.
fix
Install selectors2 and use 'from selectors2 import DefaultSelector'.
error RuntimeError: No selector implementation available
cause Platform does not support any selector (e.g., very minimal environment).
fix
Check platform support; consider using a different I/O multiplexing library.
gotcha selectors2 is not a drop-in replacement for the stdlib selectors module. It may behave differently on exotic platforms.
fix Test thoroughly on target platform; consider using stdlib if Python >=3.4 and no exotic platform needed.
deprecated Python 2 long integers are supported but Python 2 itself is EOL. Upcoming versions may drop Python 2 support.
fix Migrate to Python 3.

Create a DefaultSelector and register a listening socket for read events.

import selectors2
import socket

sel = selectors2.DefaultSelector()
sock = socket.socket()
sock.bind(('localhost', 1234))
sock.listen()
sel.register(sock, selectors2.EVENT_READ)
while True:
    events = sel.select(timeout=1)
    for key, mask in events:
        conn, addr = key.fileobj.accept()
        print(f'Connection from {addr}')