AlgoMaster Logo

Non-Blocking I/O and select()

Medium Priority30 min readUpdated August 14, 2026

A blocking server associates waiting with a thread. When a thread calls recv() and no data is available, the operating system puts that thread to sleep until the socket changes state.

Non-blocking I/O separates waiting from the socket operation itself. A non-blocking recv() returns immediately when no bytes are available. The application uses a readiness API such as select() to ask:

This changes the server architecture. Instead of one blocked worker per connection, one event-loop thread watches many sockets, performs only the operations that are ready, stores unfinished work in per-connection state, and returns to the readiness check.

This chapter builds single-threaded non-blocking TCP echo servers in Python and Java. Both servers:

  • Accept many clients through one event loop
  • Parse a length-prefixed request incrementally
  • Preserve unsent bytes after a partial write
  • Monitor write readiness only when output is pending
  • Close idle and malformed connections
  • Bound the number of admitted clients

The key idea is not that non-blocking calls are always faster. It is that a thread no longer has to remain blocked for every idle connection.

Premium Content

Subscribe to unlock full access to this content and more premium articles.