Search code examples
pythonmultiprocessingporttornado

how to bind multiple port with multiprocessing when using tornado


My python version is 3.4, my tornado version is 4.3. I have 2 servers, and they have to share some datas during runtime, my code is like this,

from tornado.web import gen, asynchronous, RequestHandler, Application
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop


class HelloHandler(RequestHandler):
    @asynchronous
    @gen.engine
    def get(self):
        self.write('hello')
        self.finish()


class MainHandler(RequestHandler):
    @asynchronous
    @gen.engine
    def get(self):
        self.write('main')
        self.finish()


helloApp = Application([
    (r'/hello', HelloHandler),
])

mainApp = Application([
    (r'/main', MainHandler),
])

if __name__ == "__main__":
    hello_server = HTTPServer(helloApp)
    hello_server.bind(8881)
    hello_server.start()
    # hello_server.start(0)
    main_server = HTTPServer(mainApp)
    main_server.bind(8882)
    main_server.start()
    # main_server.start(0)
    IOLoop.current().start()

It works, but when I tried to support multiple processes by using server.start(0), I recived an error: 'OSError: [Errno 98] Address already in use', I used different ports already (8881, 8882). How does this happen? And how to fix it?


Solution

  • start(n) only works with a single server. To use more than one, you must use bind_sockets, fork_processes, and add_sockets separately (example adapted from http://www.tornadoweb.org/en/stable/tcpserver.html):

    from tornado.netutil import bind_sockets
    
    hello_sockets = bind_sockets(8881)
    main_sockets = bind_sockets(8882)
    tornado.process.fork_processes(0)
    hello_server = HTTPServer(helloApp)
    hello_server.add_sockets(hello_sockets)
    main_server = HTTPServer(mainApp)
    main_server.add_sockets(main_sockets)
    IOLoop.current().start()