liveBook · Manning

Busy polling

As explained in Grokking Concurrency

Busy-waiting is a technique in which a task repeatedly checks for a condition instead of blocking while waiting for it.

Imagine checking the oven repeatedly instead of waiting for its timer. Busy polling does the same with I/O: a task repeatedly checks whether an operation is ready rather than blocking. section 10.11

A nonblocking polling loop typically works like this:

  • Put the socket into nonblocking mode so accept(), recv(), or send() returns immediately instead of waiting. section 10.11
  • Try the I/O operation repeatedly; Python reports an unready socket with BlockingIOError. section 10.11
  • Process completed operations while continuing to check other clients in the same thread. section 10.12
  • Accept the trade-off: polling avoids waiting and many threads, but repeated unsuccessful checks are inefficient. section 10.12 section 11.10

A nonblocking socket server

A Python server calls setblocking(False) on its listening socket and client sockets. Its endless loop tries accept(), read, and send operations, ignoring BlockingIOError when a socket is not ready and trying again later. section 10.11

How Grokking Concurrency covers it

How deep it goes
Explains nonblocking configuration, repeated operations, exceptions, and thread trade-offs, while stopping short of implementation beyond a server listing.
Examples
Demonstrates an oven analogy and a Python server polling accept, read, and send operations.
What it assumes
Assumes basic socket operations and introduces nonblocking behavior, BlockingIOError, and polling from fundamentals.
What you can do after
Operate/configure a Python nonblocking socket server that retries I/O without dedicating a thread to each client.
How it connects
Connects nonblocking I/O with sockets, threads, context switching, and event-based concurrency.

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.