Skip to content

Instantly share code, notes, and snippets.

@jholster
Created April 3, 2012 09:06
Show Gist options
  • Save jholster/2290574 to your computer and use it in GitHub Desktop.
Save jholster/2290574 to your computer and use it in GitHub Desktop.
Tornado multiprocess example
import tornado.web
import tornado.httpserver
import tornado.ioloop
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Greetings from the instance %s!" % tornado.process.task_id())
app = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
server = tornado.httpserver.HTTPServer(app)
server.bind(8888)
server.start(0) # autodetect number of cores and fork a process for each
tornado.ioloop.IOLoop.instance().start()
@shimont
Copy link

shimont commented Sep 22, 2013

Thank you very much
This is what i was looking for start(0)

from tornadoweb docs

start(num_processes=1)[source]
Starts this server in the IOLoop.

By default, we run the server in this process and do not fork any additional child process.

If num_processes is None or <= 0, we detect the number of cores available on this machine and fork that number of child processes. If num_processes is given and > 1, we fork that specific number of sub-processes.

Since we use processes and not threads, there is no shared memory between any server code.

Note that multiple processes are not compatible with the autoreload module (or the debug=True option to tornado.web.Application). When using multiple processes, no IOLoops can be created or referenced until after the call to TCPServer.start(n).

@mkyung
Copy link

mkyung commented Nov 14, 2013

The document states that it is not the advised way to fork the tornado processes.
A better way would be set up several independent tornado processes and load balanced by Nginx.

@gwillem
Copy link

gwillem commented Jun 30, 2015

@mkyung do you have a link to that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment