Search code examples
tornado

tornado multi-process HTTPServer()


I have a 4 core system and wanted to test the multi process tornado functionality. I start my program and it shows that 4 processes are started:

[I 151013 19:19:44 process:124] Starting 4 processes

My code is pretty much similar to what's listed in the documentation:

server = HTTPServer(app)
server.bind(8000)
server.start(0)  # Forks multiple sub-processes
IOLoop.current().start()

In my main handler I intentionally call a time.sleep(10) to block the process. I was under the impression that if I connect another client to the webserver with this client being blocked it will still be serviced because there's 4 processes listening on the port. And only until I reach > 4 processes of (assuming that they are all still blocking) will a client have to wait for a connection.

However when I tested this the second client has to wait for the first one to unblock. So I'm not sure I'm understanding the purpose of a multi process listening on the same port. Am I missing something? What's the purpose/ advantage of forking multi processes for the HTTPServer?


Solution

  • Sounds like you're running into a surprising browser behavior that trips up many users when they first try Tornado, read the frequently-asked "My code is asynchronous, but it’s not running in parallel in two browser tabs".

    Browsers will recognize that you are trying to load the same page in two different tabs and delay the second request until the first has finished.