tornado.wsgi — Interoperability with other Python frameworks and servers¶
WSGI support for the Tornado web framework.
WSGI is the Python standard for web servers, and allows for interoperability between Tornado and other Python web frameworks and servers.
This module provides WSGI support via the WSGIContainer class, which
makes it possible to run applications using other WSGI frameworks on
the Tornado HTTP server. The reverse is not supported; the Tornado
Application and RequestHandler classes are designed for use with
the Tornado HTTPServer and cannot be used in a generic WSGI
container.
- class tornado.wsgi.WSGIContainer(wsgi_application: WSGIAppType)[source]¶
Makes a WSGI-compatible function runnable on Tornado’s HTTP server.
Warning
WSGI is a synchronous interface, while Tornado’s concurrency model is based on single-threaded asynchronous execution. This means that running a WSGI app with Tornado’s
WSGIContaineris less scalable than running the same app in a multi-threaded WSGI server likegunicornoruwsgi. UseWSGIContaineronly when there are benefits to combining Tornado and WSGI in the same process that outweigh the reduced scalability.Wrap a WSGI function in a
WSGIContainerand pass it toHTTPServerto run it. For example:def simple_app(environ, start_response): status = "200 OK" response_headers = [("Content-type", "text/plain")] start_response(status, response_headers) return [b"Hello world!\n"] async def main(): container = tornado.wsgi.WSGIContainer(simple_app) http_server = tornado.httpserver.HTTPServer(container) http_server.listen(8888) await asyncio.Event().wait() asyncio.run(main())
This class is intended to let other frameworks (Django, web.py, etc) run on the Tornado HTTP server and I/O loop.
The
tornado.web.FallbackHandlerclass is often useful for mixing Tornado and WSGI apps in the same server. See https://github.com/bdarnell/django-tornado-demo for a complete example.- static environ(request: HTTPServerRequest) Dict[str, Any][source]¶
Converts a
tornado.httputil.HTTPServerRequestto a WSGI environment.