Ryan’s I like Unicorn because it’s Unix appears to have started a mini-meme of folks writing simple forking network servers in their language of choice. I’m really enjoying reading ‘em — they’re a sort of Rosetta Stone of network code — so I’m going to keep a running list of links here. Tell me about any I’ve missed (via email, comments, or twitter) and I’ll add ‘em to the list.
- Ruby: I like Unicorn because it’s Unix (Ryan’s original version).
- Python: Python is Unix (my original Python version).
- Python: Jeff Zellman’s improved Python version.
- Python: my more Pythonic version (using SocketServer).
- Perl: Perl is Unix (by Aristotle Pagaltzis).
- C: Unix is C (Paul Smith brings it full circle).
- PHP version by Russell Beattie.
- Haskell version by jinjing.
Comments:
The SocketServer version is s-w-e-e-t.
This is actually pretty educational for me. I implemented the sample in PHP using your Python version as a start (as I don't really know Ruby):
http://pastebin.com/f3ad81c27
It works fine, except that I can't seem to trap Ctl-C, which I'm not sure is a bug or that I'm not using the signal traps correctly.
Thanks for collecting the posts!
-Russ
very interesting :) here's a haskell one
http://gist.github.com/204859
Your SocketServer version is not pre-forking.
You can have a pre-forking server with SocketServer. Instead of using ForkingTCPServer, just use TCPServer and then fork a few times, having each child call serve_forever().
Stackless Python / Concurrence based one (not forking, not threading, but light-weight tasking...(async/epoll under the hood):
http://opensource.hyves.org...
Russell Beattie, PHP signals are not working correctly, since first socket_accept is blocking all other stuff. If you press Ctrl+C and then try to establish connection, you'll see script exiting immediately, since connection has been accepted and now PHP can continue with its ticks.
I implemented my own version, but you were first to publish it here :)
Ocaml! http://gist.github.com/205407
May not be exemplary, but it's pretty simple.
Leave a comment: