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
