当前位置: 首页 > 文档资料 > Tornado 用户手册 >

tornado.wsgi — Interoperability with other Python frameworks and servers

优质
小牛编辑
142浏览
2023-12-01

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 in two ways:

  • converts a to the WSGI application interface. This is useful for running a Tornado app on another HTTP server, such as Google App Engine. See the class documentation for limitations that apply.
  • lets you run other WSGI applications and frameworks on the Tornado HTTP server. For example, with this class you can mix Django and Tornado handlers in a single server.

Running Tornado apps on WSGI servers

class tornado.wsgi.WSGIAdapter(application)

Converts a instance into a WSGI application.

Example usage:

import tornado.web
import tornado.wsgi
import wsgiref.simple_server

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

if __name__ == "__main__":
    application = tornado.web.Application([
        (r"/", MainHandler),
    ])
    wsgi_app = tornado.wsgi.WSGIAdapter(application)
    server = wsgiref.simple_server.make_server('', 8888, wsgi_app)
    server.serve_forever()

See the appengine demo for an example of using this module to run a Tornado app on Google App Engine.

In WSGI mode asynchronous methods are not supported. This means that it is not possible to use , or the or modules.

4.0 新版功能.

class tornado.wsgi.WSGIApplication(handlers=None, default_host='', transforms=None, **settings)

A WSGI equivalent of .

4.0 版后已移除: Use a regular and wrap it in instead.

Running WSGI apps on Tornado servers

class tornado.wsgi.WSGIContainer(wsgi_application)

Makes a WSGI-compatible function runnable on Tornado’s HTTP server.

警告

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 is less scalable than running the same app in a multi-threaded WSGI server like gunicorn or uwsgi. Use only when there are benefits to combining Tornado and WSGI in the same process that outweigh the reduced scalability.

Wrap a WSGI function in a and pass it to to 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 ["Hello world!\n"]

container = tornado.wsgi.WSGIContainer(simple_app)
http_server = tornado.httpserver.HTTPServer(container)
http_server.listen(8888)
tornado.ioloop.IOLoop.current().start()

This class is intended to let other frameworks (Django, web.py, etc) run on the Tornado HTTP server and I/O loop.

The class 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)

Converts a to a WSGI environment.