On this page
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
select.select()hitsFD_SETSIZE(typically 1024) on Unix: Use selectors (DefaultSelector), which automatically pickskqueue(macOS/BSD) orepoll(Linux).- Windows
select.select()only supports sockets, not pipes or regular file descriptors. - Always handle empty
readablelists when a timeout expires.
Common Functions
- select.poll() —
poll()— Returns a polling object. - select.select() —
select(rlist, wlist, xlist, timeout=None, /)— Wait until one or more file descriptors are ready for some kind of I/O.
Classes & Objects
- select.error — Base class for I/O related errors. (Methods:
.add_note(),.with_traceback()) - select.kevent — This object is the equivalent of the struct kevent for the C API.
- select.kqueue — Kqueue syscall wrapper. (Methods:
.close(),.control(),.fileno(),.fromfd())
Constants & Attributes
select.KQ_EV_ADD(int) =1select.KQ_EV_CLEAR(int) =32select.KQ_EV_DELETE(int) =2select.KQ_EV_DISABLE(int) =8select.KQ_EV_ENABLE(int) =4select.KQ_EV_EOF(int) =32768select.KQ_EV_ERROR(int) =16384select.KQ_EV_FLAG1(int) =8192select.KQ_EV_ONESHOT(int) =16select.KQ_EV_SYSFLAGS(int) =61440select.KQ_FILTER_AIO(int) =-3select.KQ_FILTER_PROC(int) =-5select.KQ_FILTER_READ(int) =-1select.KQ_FILTER_SIGNAL(int) =-6select.KQ_FILTER_TIMER(int) =-7
Linked from
Related notes
- tuts/python3/stdlib_module/secrets Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/selectors Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/argparse Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/asyncio Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/getopt Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/io Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/os Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/select/error Linked here; shared tags: #stdlib