Csdesign
Small Python examples of socket server concurrency patterns: fork, select, and more.
Install / Use
/learn @rspivak/CsdesignREADME
Simple Examples of Concurrent Server Design in Python
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.
Client
The test client works with all servers:
Server Examples
| 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. |
Quickstart
Run a server in one terminal:
python server01.py
Run the client in another:
python client.py -i localhost -p 2000 -c 5 -t 10 -b 1024
Miscellaneous Examples
Extra 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.
Roadmap
- [x] TCP Concurrent Server, One Child per Client
- [x] TCP Concurrent Server, I/O Multiplexing (select)
- [x] TCP Preforked Server, Children Call
accept - [x] 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
Acknowledgments
-
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.
Related Skills
diffs
343.1kUse the diffs tool to produce real, shareable diffs (viewer URL, file artifact, or both) instead of manual edit summaries.
openpencil
1.9kThe world's first open-source AI-native vector design tool and the first to feature concurrent Agent Teams. Design-as-Code. Turn prompts into UI directly on the live canvas. A modern alternative to Pencil.
HappyColorBlend
HappyColorBlendVibe Project Guidelines Project Overview HappyColorBlendVibe is a Figma plugin for color palette generation with advanced tint/shade blending capabilities. It allows designers to
Flyaro-waffle-app
Waffle Delight - Full Stack MERN Application Rules & Documentation Project Overview A comprehensive waffle delivery application built with MERN stack featuring premium UI/UX, admin management, a
