0Pricing
Linux Networking & TCP/IP for Developers · Lesson

Non-Blocking Sockets and select()

Handle multiple connections in a single Python process using non-blocking sockets and the select module for scalable I/O multiplexing.

The Blocking Problem

A simple recv() call blocks until data arrives. A single-threaded server can therefore only serve one client at a time.

To handle many clients without threads, we use non-blocking sockets combined with an I/O multiplexer like select().

Making a Socket Non-Blocking

Call setblocking(False) on a socket. Now recv() and send() return immediately, raising BlockingIOError if they would have blocked.

import socket
s = socket.socket()
s.setblocking(False)
print('blocking mode:', s.getblocking())

All lessons in this course

  1. Introduction to Socket API
  2. TCP Client-Server Sockets
  3. UDP Client-Server Sockets
  4. Non-Blocking Sockets and select()
← Back to Linux Networking & TCP/IP for Developers