Search code examples
djangoormtornadodjango-wsgi

What is the best way to run Django on Tornado Web Server to have async + django admin + django orm possibilities?


I would like to have django admin panel with tornado backends, which will process requests from online game. I dont know at the moment, is it a good idea to load django app in the next way:

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()

HelloHandler is going to be a backend parser.

Will I loose some performance in combining wsgi + ioloop ?

If its a bad solution, its possible to run 2 apps: django admin and tornado web. Could you answer how can I use Django ORM with Tornado App?


Solution

  • Just take this equation and solve it.

    • You want to have non-blocking IO - X.
    • You want to have django ORM - Y.
    • You want to have django admin - Z.

    Now, move point by point:

    • For X - tornado is enough itself.
    • For Z - django is enough itself. I don't think, you want to have thousands of administrators, that manage your site at one time.
    • For Y it is harder. Django ORM is blocking itself.

    Best way here - not to use Django ORM with tornado.

    Second way - you can dive deeper and integrate it directly in tornado, if you are sure that it will not block whole application. Take solution from this answer.

    Third way - you can convert your django application to service, that makes heavy work with ORM, and you can access this service with AsyncHTTPCLient from tornado.

    Fourth way - integrate tornado web server into your django application. Actually, it will give you small performance boost.

    Fifth way - use two tornado web servers. Sounds crazy, yes. Use one with Django ORM integrated, and second with AsyncHTTPClient.

    I believe, that you can take best of 2 worlds.