liveBook · Manning

Select

As explained in Grokking Concurrency

select is an operating-system interface that waits for one or more file descriptors or sockets to become ready for I/O.

Imagine a receptionist watching several phones: instead of checking every line repeatedly, the receptionist waits and announces which ones need attention. select gives an operating-system event loop the same ability. It monitors registered sockets or file descriptors and blocks until one or more become ready for reading or writing. section 11.11 section 11.4

The wait applies to the notification, not to the eventual I/O operation. When sockets are already ready, select returns immediately; otherwise, it sleeps until an event arrives. The application can then process several sockets from one thread without repeatedly polling each socket itself. section 11.11 section 11.4

A typical event-driven arrangement follows this rhythm:

  • Register sockets and the read or write events they should monitor. section 11.4
  • Call select from the event loop and wait for ready events. section 11.4 section 11.5
  • Invoke the callback associated with each returned event, then return to select when pending work is finished. section 11.5

A single-threaded pizza server

A pizza server can register its listening socket and client sockets, then use callbacks such as _on_accept(), _on_read(), and _on_write(). select identifies sockets ready for work; the event loop dispatches the matching callback, allowing multiple clients to make progress without multiple server threads. section 11.5

Use the event-multiplexing figure to visualize how monitored sockets, queued readiness events, and the event loop fit together. section 11.4

How Grokking Concurrency covers it

How deep it goes
Explains readiness, blocking behavior, registration, and callback dispatch, while deferring operating-system-specific efficiency details.
Examples
Demonstrates a single-threaded pizza server that dispatches accept, read, and write callbacks for multiple clients.
What it assumes
Assumes sockets and I/O events, while introducing OS-managed monitoring instead of application-level busy-waiting.
What you can do after
Build an event loop that monitors multiple sockets and dispatches handlers when their I/O becomes ready.
How it connects
Connects I/O multiplexing, nonblocking I/O, event loops, callbacks, sockets, and reactor patterns.

Related concepts

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