Back
+20 XP
Concept

I/O Multiplexing with the selectors Module

Sequential socket polling wastes CPU cycles. Standard library `selectors.DefaultSelector` selects the optimal platform-native event poller (`epoll` on Linux, `kqueue` on macOS, `select` on Windows), sleeping until file descriptors become ready for I/O 🌐.

python
import selectors
import socket

sel = selectors.DefaultSelector()
s1, s2 = socket.socketpair()
s1.setblocking(False)

# Register interest in read readiness:
sel.register(s1, selectors.EVENT_READ, data="client_connection")
s2.sendall(b"NETWORK_PAYLOAD")

events = sel.select(timeout=1.0)
for key, mask in events:
    print(f"Ready: {key.data} (Mask: {mask}) 🌐")

sel.unregister(s1)
sel.close()
s1.close()
s2.close()

# 2. I/O Multiplexing with the selectors Module Sequential socket polling wastes CPU cycles. Standard library `selectors.DefaultSelector` selects the optimal platform-native event poller (`epoll` on Linux, `kqueue` on macOS, `select` on Windows), sleeping until file descriptors become ready for I/O 🌐. ```python import selectors import socket sel = selectors.DefaultSelector() s1, s2 = socket.socketpair() s1.setblocking(False) # Register interest in read readiness: sel.register(s1, selectors.EVENT_READ, data="client_connection") s2.sendall(b"NETWORK_PAYLOAD") events = sel.select(timeout=1.0) for key, mask in events: print(f"Ready: {key.data} (Mask: {mask}) 🌐") sel.unregister(s1) sel.close() s1.close() s2.close() ``` While legacy `select()` is hard-limited to 1,024 file descriptors and rescans the entire array on every tick ($O(N)$), Linux `epoll` and BSD `kqueue` maintain kernel-registered interest lists ($O(1)$), allowing servers to juggle 100,000+ open sockets with ease 🚀! --- ### Question Why is `selectors.DefaultSelector()` preferred over calling raw `select.select()` directly? 🧠 - [ ] a. It automatically picks $O(1)$ event engines like `epoll` or `kqueue` tailored to the host OS rather than $O(N)$ linear table scanning 🌐 - [ ] b. It offloads socket state verification to dedicated kernel threads - [ ] c. It eliminates the need to configure sockets as non-blocking - [ ] d. It converts all incoming byte payloads into UTF-8 strings automatically --- ### Challenge Register `s1` with `selectors.DefaultSelector()` for `EVENT_READ`. Send `b'OK'` via `s2`. Call `sel.select(timeout=1.0)` and print `len(events)`. Clean up all sockets and selector. 🎯 <details> <summary>Solution</summary> ```python import selectors import socket sel = selectors.DefaultSelector() s1, s2 = socket.socketpair() s1.setblocking(False) sel.register(s1, selectors.EVENT_READ) s2.sendall(b'OK') events = sel.select(timeout=1.0) print(len(events)) sel.unregister(s1) sel.close() s1.close() s2.close() ``` </details>

Checkpoint

Why is `selectors.DefaultSelector()` preferred over calling raw `select.select()` directly? 🧠

Practice

Register `s1` with `selectors.DefaultSelector()` for `EVENT_READ`. Send `b'OK'` via `s2`. Call `sel.select(timeout=1.0)` and print `len(events)`. Clean up all sockets and selector. 🎯

Terminal
01
Tap code blocks below to assemble
Available Blocks