What is the best way to run Django on a Tornado web server to use async + django admin + django orm features?

I would like to have a django admin panel with tornado backends that will handle requests from an online game. At the moment, I don't know if it is useful to download a django application as follows:

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 will be the main parser.

Will I lose some performance when combining wsgi + ioloop?

If this is a bad solution, you can run 2 applications: django admin and tornado web. Could you please answer how I can use Django ORM with the Tornado App?

+7
source share
2 answers

Just take this equation and solve it.

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

Now move point by point:

  • For X, a tornado is enough.
  • For Z - django is enough. I don’t think you want to have thousands of administrators who manage your site at a time.
  • For Y, this is harder. Django ORM blocks itself.

The best way here is to not use Django ORM with a tornado.

The second way - you can dive deeper and integrate it directly into the tornado, if you are sure that it will not block the entire application. Take a decision from this answer.

The third way is that you can convert your django application to a service, which does the hard work with ORM, and you can access that service using AsyncHTTPCLient from a tornado.

Fourth way - integrate the tornado web server into your django application. In fact, this will give you a small performance boost.

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

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

+14
source

Django is not asynchronous, so running Django on Tornado will remove most of the performance benefits you can get from Tornado.

If you need maximum async performance, you should use Tornado with a non-blocking database (I assume you want the Django admin to be used with the SQL database).

If you need maximum ease of development, use Django, with it both ORM systems and administration tools.

You can't just mix the best of both worlds, unfortunately.

So yes, you will lose performance. In this situation, I would probably use Tornado and abandon the Django admin. If you are willing to compromise, you can write two applications using the database, but this will mean that you need to support two levels of data access.

+5
source

All Articles