在 Tornado Web 服务器上运行 Django 以实现异步的最佳方式是什么? Django 管理 + django orm 的可能性?

发布于 2024-12-24 18:39:41 字数 619 浏览 6 评论 0原文

我想要带有龙卷风后端的 django 管理面板,它将处理来自在线游戏的请求。我现在不知道以下一种方式加载 django 应用程序是个好主意:

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 将成为后端解析器。

组合 wsgi + ioloop 会失去一些性能吗?

如果它是一个糟糕的解决方案,则可以运行 2 个应用程序:django admin 和 Tornado Web。你能回答我如何将 Django ORM 与 Tornado 应用程序一起使用吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别在捏我脸啦 2024-12-31 18:39:41

只要把这个方程解出来就可以了。

  • 你想要非阻塞 IO - X。
  • 你想要 django ORM - Y。
  • 你想要 django admin - Z。

现在,逐点移动:

  • 对于 X - 龙卷风本身就足够了。
  • 对于 Z - django 本身就足够了。我不认为,你想要拥有
    数千名管理员同时管理您的站点。
  • 对于Y来说更难。 Django ORM 正在阻塞自身。

这里最好的方法 - 不要将 Django ORM 与tornado一起使用。

第二种方法 - 如果您确定它不会阻塞整个应用程序,您可以更深入地研究并将其直接集成到tornado中。从这个答案中获取解决方案。

第三种方式 - 您可以将 django 应用程序转换为服务,这使得 ORM 工作繁重,并且您可以使用来自tornado的 AsyncHTTPCLient 访问此服务。

第四种方法 - 将 Tornado Web 服务器集成到您的 Django 应用程序中。事实上,它会给你带来小小的性能提升。

第五种方法 - 使用两个龙卷风网络服务器。听起来很疯狂,是的。第一个使用集成了 Django ORM,第二个使用 AsyncHTTPClient

我相信,你可以两全其美。

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.

神魇的王 2024-12-31 18:39:41

Django 不是异步的,因此在 Tornado 中运行 Django 将消除您可能从 Tornado 中获得的大部分性能优势。

如果您想要最大的异步性能,您应该将 Tornado 与非阻塞数据库一起使用(我假设您希望 Django admin 与 SQL 数据库一起使用)。

如果您想要最大程度地简化开发,请使用 Django 及其 ORM 系统和管理工具。

不幸的是,你不能将两全其美结合起来。

所以,是的,你会失去性能。在这种情况下,我可能会使用 Tornado 并放弃 Django admin。如果您坚决要妥协,您可以编写两个应用程序,共享一个数据库,但这意味着您需要维护两个数据访问层。

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

If you want maximum async performance, you should use Tornado with a non-blocking database (I'm assuming you want Django admin for use with a SQL database).

If you want maximum ease of development, use Django, with it's ORM system and admin 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 give up on Django admin. If you're dead set on a compromise you could write two apps, sharing a database, but that will mean you need to maintain two data access layers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文