Categories: Python Tornado教程

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

前端小白,在Web176教程网这个平台跟大家一起学习,加油!

Share
Published by
andy

Recent Posts

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

6 小时 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

1 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

1 周 ago

Vue3:手动清理keep-alive组件缓存的方法

Vue3中手动清理keep-a…

2 周 ago

聊聊React和Vue组件更新的实现及区别

React 和 Vue 都是当…

3 周 ago