How use Django with Tornado web server?
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 osimport tornado.httpserverimport tornado.ioloopimport tornado.wsgiimport sysimport 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 osimport tornado.httpserverimport tornado.ioloopimport tornado.wsgifrom django.core.wsgi import get_wsgi_applicationdef 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()
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 osimport tornado.httpserverimport tornado.ioloopimport tornado.wsgiimport sysimport django.core.handlers.wsgifrom 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()
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;}