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.

Non-Blocking Sockets and select() is a free Linux Networking & TCP/IP for Developers lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Linux Networking & TCP/IP for Developers learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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())

Introducing select()

select.select() watches lists of sockets and tells you which are ready.

It takes three lists: read, write, and error, and returns the subsets that are ready.

import select
readable, writable, errored = select.select(inputs, outputs, inputs)

The Server Setup

Create a listening socket, set it non-blocking, and add it to the inputs list. The listening socket becoming 'readable' means a new connection is waiting.

server = socket.socket()
server.setblocking(False)
server.bind(('localhost', 9000))
server.listen()
inputs = [server]

Accepting New Connections

When the listening socket is in the readable list, call accept(). Make the new client socket non-blocking and add it to inputs too.

if s is server:
    conn, addr = server.accept()
    conn.setblocking(False)
    inputs.append(conn)

Reading from Clients

For any other readable socket, call recv(). An empty bytes result means the client closed the connection, so remove it from inputs and close it.

data = s.recv(1024)
if data:
    print('got', data)
else:
    inputs.remove(s)
    s.close()

The Full Event Loop

Wrap accept and read logic in a while True loop driven by select(). This is the heart of an event-driven server.

while inputs:
    readable, _, _ = select.select(inputs, [], [])
    for s in readable:
        handle(s)

Handling Writes

For high-throughput servers you also track the writable list. Only send when the OS says the socket can accept data, avoiding partial-send stalls.

readable, writable, _ = select.select(inputs, outputs, [])
for s in writable:
    s.send(pending[s])

Beyond select: poll and epoll

select() is limited by the FD_SETSIZE constant and scans all sockets each call.

For thousands of connections, Linux offers select.poll() and the more scalable select.epoll(), which scale far better.

Where selectors Fits In

The high-level selectors module wraps the best available backend (epoll/kqueue) behind one API. Most production code uses it instead of raw select.

import selectors
sel = selectors.DefaultSelector()
sel.register(server, selectors.EVENT_READ)

Timeouts

Passing a timeout to select() lets the loop wake periodically even with no I/O, useful for housekeeping. A timeout of 0 polls without blocking.

readable, _, _ = select.select(inputs, [], [], 1.0)
if not readable:
    print('no activity this second')

Quick Check

Test your multiplexing knowledge.

Recap

You can now build a single-threaded multi-client server:

  • setblocking(False) for non-blocking sockets
  • select() to discover ready sockets
  • accept on the listening socket, recv on clients
  • Scale up with poll, epoll, or the selectors module

This builds on your TCP and UDP socket lessons toward scalable network services.

Frequently asked questions

Is the “Non-Blocking Sockets and select()” lesson free?

Yes — the full text of “Non-Blocking Sockets and select()” is free to read here on the web, and the Linux Networking & TCP/IP for Developers course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Linux Networking & TCP/IP for Developers course, upgrade to CoddyKit PRO.

What will I learn in “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. You practise Linux Networking & TCP/IP for Developers with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Linux Networking & TCP/IP for Developers?

No prior experience is required. Linux Networking & TCP/IP for Developers on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Non-Blocking Sockets and select()” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Linux Networking & TCP/IP for Developers lesson?

Yes. Every Linux Networking & TCP/IP for Developers lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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