On this page
  1. select
  2. Import
  3. Overview
  4. Quick Start
  5. Implementation Walkthrough
  6. 1. Non-Blocking Readiness Check with Explicit Timeout
  7. Gotchas & Boundaries
  8. Common Functions
  9. Classes & Objects
  10. Constants & Attributes

select

Prev: secrets | Up: stdlib_module | Next: selectors
Rel: os, io, time, argparse, getopt
Ref: docs.python.org — select
Tags: #public #python3 #stdlib


Import

import select

Overview

Low-level OS I/O multiplexing primitives (select, poll, epoll, kqueue) that wait efficiently for multiple sockets or file descriptors to become readable or writable. For new Python code, prefer the higher-level portable selectors module (or asyncio for application concurrency) rather than calling select.select() directly.

Quick Start

import select
import socket

s1, s2 = socket.socketpair()
try:
    s1.sendall(b"ping")
    readable, _, _ = select.select([s2], [], [], 1.0)
    assert readable == [s2] and s2.recv(4) == b"ping"
finally:
    s1.close()
    s2.close()

Implementation Walkthrough

1. Non-Blocking Readiness Check with Explicit Timeout

Pass timeout=0.0 for an immediate non-blocking poll, or a positive float for bounded waiting:

import select
import socket

def has_pending_data(sock: socket.socket, timeout: float = 0.05) -> bool:
    readable, _, _ = select.select([sock], [], [], timeout)
    return bool(readable)

Gotchas & Boundaries

Common Functions

Classes & Objects

Constants & Attributes

Linked from

Related notes