Send socket object to forked running process (multiprocessing.Queue)

后端 未结 4 994
臣服心动
臣服心动 2021-01-18 14:14

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

相关标签:
4条回答
  • 2021-01-18 14:33

    If you create socket before process forking, then its file descriptor will be inherited by the children.

    0 讨论(0)
  • 2021-01-18 14:41

    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.

    0 讨论(0)
  • 2021-01-18 14:52

    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.

    0 讨论(0)
  • 2021-01-18 14:54

    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

    0 讨论(0)
提交回复
热议问题