Search code examples
pythondjangotornadowaitress

How to serve Tornado with Django on waitress?


I have this tornado application wrapped with django function as WSGI app (using in windows)

from django.core.wsgi import get_wsgi_application
from django.conf import settings
from waitress import serve
settings.configure()

wsgi_app = tornado.wsgi.WSGIContainer(django.core.wsgi.WSGIHandler())
def tornado_app():  
    url = [(r"/models//predict", PHandler),
           (r"/models//explain", EHandler),
           ('.*', tornado.web.FallbackHandler, dict(fallback=wsgi_app))]
    return Application(url, db=ELASTIC_URL, debug=options.debug, autoreload=options.debug)

if __name__ == "__main__":
    application = tornado_app()
    http_server = HTTPServer(application)  
    http_server.listen(LISTEN_PORT)
    IOLoop.current().start() 

Not sure how to use waitress, To serve using waitress I tried http_server = serve(application), server is starting, now sure is it correct, getting error when hitting the endpoint

enter image description here


Solution

  • waitress is a WSGI server; Tornado is not based on or compatible with WSGI. You cannot use waitress to serve the Tornado portions of your application.

    To serve both Tornado and WSGI applications in one thread, you need to use Tornado's HTTPServer as you've done in the original example. For better scalability, I'd recommend splitting the Tornado and Django portions of your application into separate processes and putting a proxy like nginx or haproxy in front of them.