Tornado 与其他Python框架和服务器的互操作性

WSGI 是 Web 服务器的 Python 标准,允许 Tornado 与其他 Python Web 框架和服务器之间的互操作性。

该模块通过 ​WSGIContainer类提供 WSGI 支持,这使得使用 Tornado HTTP 服务器上的其他 WSGI 框架运行应用程序成为可能。 不支持反向; Tornado ​Application​和 ​RequestHandler类是为 Tornado ​HTTPServer设计的,不能在通用 WSGI 容器中使用。

class tornado.wsgi.WSGIContainer(wsgi_application: WSGIAppType)

使 WSGI 兼容的函数可在 Tornado 的 HTTP 服务器上运行。

注意:

WSGI 是一个同步接口,而 Tornado 的并发模型是基于单线程异步执行的。 这意味着使用 Tornado 的 ​WSGIContainer运行 WSGI 应用程序的可扩展性低于在多线程 WSGI 服务器(如 ​gunicorn或 ​uwsgi​)中运行相同的应用程序。 仅当在同一进程中组合 Tornado 和 WSGI 的好处大于降低的可伸缩性时,才使用 ​WSGIContainer​。

在 ​WSGIContainer中包装一个 WSGI 函数并将其传递给 ​HTTPServer以运行它。 例如:

def simple_app(environ, start_response):
    status = "200 OK"
    response_headers = [("Content-type", "text/plain")]
    start_response(status, response_headers)
    return ["Hello world!
"]

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 循环上运行。

tornado.web.FallbackHandler​ 类通常对于在同一服务器中混合 Tornado 和 WSGI 应用程序很有用。

static environ(request: tornado.httputil.HTTPServerRequest) → Dict[str, Any]

将 ​tornado.httputil.HTTPServerRequest​ 转换为 WSGI 环境。

作者:andy,如若转载,请注明出处:https://www.web176.com/tornado/20747.html

(0)
打赏 支付宝 支付宝 微信 微信
andy的头像andy
上一篇 2023年5月18日
下一篇 2023年5月18日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注