WSGI 是 Web 服務器的 Python 標準,允許 Tornado 與其他 Python Web 框架和服務器之間的互操作性。
該模塊通過 ?WSGIContainer
類提供 WSGI 支持,這使得使用 Tornado HTTP 服務器上的其他 WSGI 框架運行應用程序成為可能。 不支持反向; Tornado ?Application
?和 ?RequestHandler
類是為 Tornado ?HTTPServer
設計的,不能在通用 WSGI 容器中使用。
使 WSGI 兼容的函數(shù)可在 Tornado 的 HTTP 服務器上運行。
注意:
WSGI 是一個同步接口,而 Tornado 的并發(fā)模型是基于單線程異步執(zhí)行的。 這意味著使用 Tornado 的 ?WSGIContainer
運行 WSGI 應用程序的可擴展性低于在多線程 WSGI 服務器(如 ?gunicorn
或 ?uwsgi
?)中運行相同的應用程序。 僅當在同一進程中組合 Tornado 和 WSGI 的好處大于降低的可伸縮性時,才使用 ?WSGIContainer
?。
在 ?WSGIContainer
中包裝一個 WSGI 函數(shù)并將其傳遞給 ?HTTPServer
以運行它。 例如:
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()
此類旨在讓其他框架(Django、web.py 等)在 Tornado HTTP 服務器和 I/O 循環(huán)上運行。
?tornado.web.FallbackHandler
? 類通常對于在同一服務器中混合 Tornado 和 WSGI 應用程序很有用。
將 ?tornado.httputil.HTTPServerRequest
? 轉換為 WSGI 環(huán)境。
更多建議: