concurrent connections in tornado
Question: I have a server running on tornado. I have a page that opens a `websocket` to
the same server. Now I have observed that opening multiple instances of this
page makes all of them wait except one. Only after that one has finished its
websocket, does another one start. Is this normal tornado behaviour of I'm
doing something wrong?

Earlier my server was running with `django` but I migrated to `tornado` for
the websocket support. For that I use fallback server as django.

    
    
    #!/usr/bin/env python
    
    # Run this with
    # PYTHONPATH=. DJANGO_SETTINGS_MODULE=testsite.settings testsite/tornado_main.py
    # Serves by default at
    # http://localhost:8080/hello-tornado and
    # http://localhost:8080/hello-django
    
    from tornado.options import options, define, parse_command_line
    import django.core.handlers.wsgi
    import tornado.httpserver
    import tornado.ioloop
    import tornado.web
    import tornado.wsgi
    
    define('port', type=int, default=8000)
    
    class HelloHandler(tornado.web.RequestHandler):
      def get(self):
        self.write('Hello from tornado')
    
    def main():
      wsgi_app = tornado.wsgi.WSGIContainer(
        django.core.handlers.wsgi.WSGIHandler())
      tornado_app = tornado.web.Application(
        [
          ('/hello-tornado', HelloHandler),
          ('.*', tornado.web.FallbackHandler, dict(fallback=wsgi_app)),
          ])
      server = tornado.httpserver.HTTPServer(tornado_app)
      server.listen(options.port)
      tornado.ioloop.IOLoop.instance().start()
    
    if __name__ == '__main__':
      main()
    

Can I do something that can allow me to make multiple connections?


Answer: You need to look into the Asych facilities in tornado to get this to work
properly. Tornado in it's normal state is a single threaded stack and thus you
can only handle one connection at a time.

You can use the normal @asynchronous decorator or use their gen library to
allow your code to handle multiple connections.

Decorator: <http://www.tornadoweb.org/documentation/web.html#decorators>

Gen: <http://www.tornadoweb.org/documentation/gen.html>

Read the documentation carefully if you choose to use the @asynchronous
decorator as you need to close the connection when you are done with it.

