如何将 Django 与 Tornado Web 服务器一起使用?

发布于 2024-08-26 21:17:37 字数 40 浏览 5 评论 0原文

如何将 Django 与 Tornado Web 服务器一起使用?

How do I use Django with the Tornado web server?

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

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

发布评论

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

评论(4

何处潇湘 2024-09-02 21:17:37

它非常简单(尤其是 django 1.4)。

1 - 只需构建您的 django 项目(和应用程序)并确保其正常工作。

2- 在根文件夹(与使用 django-admin.py startproject 的目录相同的目录)创建一个新的 python 文件

3- 然后复制下面的代码,编辑 os.environ['DJANGO_SETTINGS_MODULE '] 行,并将其粘贴到新的 .py 文件中。

import os
import tornado.httpserver
import tornado.ioloop
import tornado.wsgi
import sys
import django.core.handlers.wsgi
#sys.path.append('/home/lawgon/') # path to your project ( if you have it in another dir).


def main():
    os.environ['DJANGO_SETTINGS_MODULE'] = 'myProject.settings' # path to your settings module
    application = django.core.handlers.wsgi.WSGIHandler()
    container = tornado.wsgi.WSGIContainer(application)
    http_server = tornado.httpserver.HTTPServer(container)
    http_server.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":
    main()

Django 1.6+ 应该是这样的:

import os
import tornado.httpserver
import tornado.ioloop
import tornado.wsgi
from django.core.wsgi import get_wsgi_application

def main():
    os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings' # path to your settings module
    application = get_wsgi_application()
    container = tornado.wsgi.WSGIContainer(application)
    http_server = tornado.httpserver.HTTPServer(container)
    http_server.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":
    main()

it's very simple ( especially with django 1.4) .

1 - just build your django project( and apps ) and make sure it works fine.

2- create a new python file at the root folder ( same dir where you used django-admin.py startproject)

3- then copy the code below , edit the os.environ['DJANGO_SETTINGS_MODULE'] line, and paste it in that new .py file.

import os
import tornado.httpserver
import tornado.ioloop
import tornado.wsgi
import sys
import django.core.handlers.wsgi
#sys.path.append('/home/lawgon/') # path to your project ( if you have it in another dir).


def main():
    os.environ['DJANGO_SETTINGS_MODULE'] = 'myProject.settings' # path to your settings module
    application = django.core.handlers.wsgi.WSGIHandler()
    container = tornado.wsgi.WSGIContainer(application)
    http_server = tornado.httpserver.HTTPServer(container)
    http_server.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":
    main()

Django 1.6+ it should be like this:

import os
import tornado.httpserver
import tornado.ioloop
import tornado.wsgi
from django.core.wsgi import get_wsgi_application

def main():
    os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings' # path to your settings module
    application = get_wsgi_application()
    container = tornado.wsgi.WSGIContainer(application)
    http_server = tornado.httpserver.HTTPServer(container)
    http_server.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":
    main()
祁梦 2024-09-02 21:17:37

更新:

我创建了一个最小的工作演示,展示了如何使用 Tornado Web 服务器与 django 完美运行:

https://github.com/tamasgal/django-tornado

原始帖子:

备注:WSGI 应用程序工作流程已从 1.6 更改为 1.7。您必须将导入替换

import django.core.handlers.wsgi

from django.core.wsgi import get_wsgi_application

并将应用程序初始化从更改

application = django.core.handlers.wsgi.WSGIHandler()

application = get_wsgi_application()

这是 Moayyad Yaghi 的答案中修改后的代码:

import os
import tornado.httpserver
import tornado.ioloop
import tornado.wsgi
import sys
import django.core.handlers.wsgi
from django.core.wsgi import get_wsgi_application
#sys.path.append('/home/lawgon/') # path to your project ( if you have it in another dir).


def main():
    os.environ['DJANGO_SETTINGS_MODULE'] = 'myProject.settings' # path to your settings module
    application = django.core.handlers.wsgi.WSGIHandler()
    application = get_wsgi_application()
    container = tornado.wsgi.WSGIContainer(application)
    http_server = tornado.httpserver.HTTPServer(container)
    http_server.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":
    main()

UPDATE:

I created a minimal working demo which shows how to use the Tornado web server to run nicely with django:

https://github.com/tamasgal/django-tornado

ORIGINAL POST:

Just a remark: The WSGI application workflow has been changed from 1.6 to 1.7. You have to replace the import

import django.core.handlers.wsgi

with

from django.core.wsgi import get_wsgi_application

and change the application initialisation from

application = django.core.handlers.wsgi.WSGIHandler()

to

application = get_wsgi_application()

This is the modified code from the Moayyad Yaghi's answer:

import os
import tornado.httpserver
import tornado.ioloop
import tornado.wsgi
import sys
import django.core.handlers.wsgi
from django.core.wsgi import get_wsgi_application
#sys.path.append('/home/lawgon/') # path to your project ( if you have it in another dir).


def main():
    os.environ['DJANGO_SETTINGS_MODULE'] = 'myProject.settings' # path to your settings module
    application = django.core.handlers.wsgi.WSGIHandler()
    application = get_wsgi_application()
    container = tornado.wsgi.WSGIContainer(application)
    http_server = tornado.httpserver.HTTPServer(container)
    http_server.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":
    main()
请远离我 2024-09-02 21:17:37

有一个名为 tornado-proxy 的项目可以帮助您。但我建议您使用 Nginx。在 Nginx 配置中,您现在可以使用 proxy_pass 来引导您的调用,如下所示:

location /comet {
  proxy_pass http://localhost:8081;
}

location / {
  proxy_pass http://localhost:8080;
}

There's a project called tornado-proxy that would help you. But I would like to recommend that you use Nginx. In the Nginx config you could now use proxy_pass to direct your calls like this:

location /comet {
  proxy_pass http://localhost:8081;
}

location / {
  proxy_pass http://localhost:8080;
}
人│生佛魔见 2024-09-02 21:17:37

在现实世界中,您可以使用 WSGI。此演示展示了如何从一个提供不同 URL 前缀的 Python 模块并排运行 Tornado(及其网络服务器)和 Django:https://github.com/bdarnell/django-tornado-demo
不过,Tornado 会阻止向 Django 提供任何请求。

In real world you would connect Django and some production-ready webserver with WSGI. This demo shows how you can run Tornado (and it's webserver) and Django side by side from one python module serving different URL prefixes: https://github.com/bdarnell/django-tornado-demo.
Tornado would block on serving any request directed to Django, though.

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