I am learning to use HTML5 WebSockets and as part of that I am writing a server in Python so I can know the nitty gritty of how they work. I created one the other day that w
If you create socket before process forking, then its file descriptor will be inherited by the children.
You can't send sockets to forked processes without using deep unix magic. You can read the book Advanced Programming in the Unix environment if you really want to see how it is done in C.
For an easy solution change to using threading and your code will probably just work. While threading is not perfect in Python it is pretty good for doing IO intensive stuff.
Alternatively create your listening socket before forking and get all the subprocesses to listen (call accept
). The os will ensure that only one of them gets the connection. This is the way preforking servers are usually written.
It's been around for 4+ years, though takes a little bit of work.
The guts are hanging around in multiprocessing.reduction, and the details of an example can be seen in this github gist.
Pass socket to another process is not a trivial thing. Look, for example, this question: Can I open a socket and pass it to another process in Linux
Anyway, OS processes or threads is not what you really want for implement websocket server, because of large memory overhead. Look at smth with nonblocking sockets... eg, Tornado http://www.tornadoweb.org/documentation/websocket.html