I found this example (see below or
link) in an open source project which fork some processes and sets up several http servers.
As far as I understand Tornado will fork 4 process (I have 4 cores, and the argument to fork_processes is 0) will exit the parent that created the fork as I read in the
code. What I think the result is going to be
is that all 4 process will be accepting connection on 8888 and one of the child process (task_id == 0) will also have two more servers that listen on 7777 and 9999. Is that right?
What I don't understand is what happens to the socket listing on port 8888? Are connections being distributed across all processes? Does the kernel distribute connections? They just go to one process? I thought that only one processes can listen then you fork when accepting (of course this is not tornado model, that I understand, but I don't understand what _is_ tornado doing?).
I've read in the list that's better to use a load balancer and supervisor. I would like to understand what happens.
def tornado_run():
# Bind tcp port to launch processes on requests
sockets = netutil.bind_sockets(8888)
# Fork working processes
process.fork_processes(0)
# Tornado app implementation
app = Application([url(r"/", RequestHandler)])
# Start http servers and attach the web app to it
server = httpserver.HTTPServer(app)
server.add_sockets(sockets)
# Perform following actions only in the parent process
process_counter = process.task_id()
if (process_counter == 0):
server_0 = Application([url(r"/", ZeroRequestHandler)])
server_0.listen(7777)
server_evt = Application([url(r"/", ZeroRequestHandler)])
server_evt.listen(9999)
# main io loop. it will loop waiting for requests
IOLoop.instance().start()