This repository contains minimal, educational examples of different concurrent server design patterns in Python. Each server demonstrates a classic approach used in Unix/Linux systems programming: forking, multiplexing, preforking, and more.
The goal: keep the code small and focused so you can learn the concepts without distractions.
The test client works with all servers:
| Example | Technique | Notes |
|---|---|---|
| server01.py | One child per client (fork) |
Simple, but resource-heavy with many clients |
| server02.py | I/O multiplexing (select) |
Efficient single-process model |
| server03.py | Preforked, children call accept |
Demonstrates the Thundering Herd problem — multiple children wake on the same listening socket, but only one accepts. Details |
| server03a.py | Preforked, connection distribution demo | Shows how Linux distributes connections |
| server04.py | Parent accepts, passes socket to child | Avoids the Thundering Herd problem by handling accept in the parent and passing the connected socket to a child. |
Run a server in one terminal:
python server01.pyRun the client in another:
python client.py -i localhost -p 2000 -c 5 -t 10 -b 1024Extra socket programming tricks and demos are available in the misc/ folder:
-
RST Packet Generation
Demonstrates how to send TCP RST packets usingSO_LINGER. -
SIGPIPE Demo
Shows what happens when writing to a reset socket. -
Self-Pipe Trick
Classic technique to avoid race conditions withselect. -
Sendfile Optimization
Efficient file transfers using thesendfile(2)system call.
- TCP Concurrent Server, One Child per Client
- TCP Concurrent Server, I/O Multiplexing (select)
- TCP Preforked Server, Children Call
accept - TCP Preforked Server, Descriptor Passing
- TCP Concurrent Server, One Thread per Client
- TCP Concurrent Server, I/O Multiplexing (poll)
- TCP Concurrent Server, I/O Multiplexing (epoll)
- TCP Prethreaded Server
- TCP_CORK socket option examples
- Documentation for every example
-
Unix Network Programming, Volume 1: The Sockets Networking API (3rd Edition) by W. Richard Stevens, Bill Fenner, Andrew M. Rudoff
The classic. Many techniques here are drawn from this book. -
The Linux Programming Interface by Michael Kerrisk
Another excellent reference.