liveBook · Manning

DefaultSelector

As explained in Python Concurrency with asyncio

DefaultSelector is a selectors implementation that automatically selects the most efficient available operating-system selector.

DefaultSelector automatically chooses the most efficient available operating-system selector, keeping selector-based socket code portable across systems. section 3.4

Use it as the coordination point for a non-blocking socket event loop:

  • Create a DefaultSelector and register sockets with the read or write events that matter. section 3.4
  • Call select with an optional timeout; it waits efficiently and returns sockets ready for processing. section 3.4
  • Process connections and client data, then register or deregister sockets as their monitoring needs change. section 3.4
Listing 3.7 Using selectors to build a non-blocking server
import selectors
import socket
from selectors import SelectorKey
from typing import List, Tuple
 
selector = selectors.DefaultSelector()
 
server_socket = socket.socket()
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
 
server_address = ('127.0.0.1', 8000)
server_socket.setblocking(False)
server_socket.bind(server_address)
server_socket.listen()
 
selector.register(server_socket, selectors.EVENT_READ)
 
while True:
    events: List[Tuple[SelectorKey, int]] = selector.select(timeout=1)  #1
 
    if len(events) == 0:                                                #2
        print('No events, waiting a bit more!')
 
    for event, _ in events:
        event_socket = event.fileobj                                    #3
 
        if event_socket == server_socket:                               #4
            connection, address = server_socket.accept()
            connection.setblocking(False)
            print(f"I got a connection from {address}")
            selector.register(connection, selectors.EVENT_READ)         #5
        else:
            data = event_socket.recv(1024)                              #6
            print(f"I got some data: {data}")
            event_socket.send(data)

Builds a non-blocking echo server with DefaultSelector, a one-second timeout, server-socket registration, and client-event handling. section 3.4

How Python Concurrency with asyncio covers it

How deep it goes
Defines selection, registration, deregistration, and timeouts, then applies them in an echo server without descending into OS-specific implementations.
Examples
Uses a non-blocking echo server that monitors a listening socket and client connections.
What you can do after
Build a portable non-blocking socket server that waits for events without stressing the CPU.
How it connects
Connects DefaultSelector to BaseSelector, the selectors module, sockets, event loops, and asyncio concurrency.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Concurrency with asyncio. The section links point to the parts of the book it draws on.