Tornado:应用程序或 HTTPServer

Tornado: Application or HTTPServer

各种 Tornado 文档显示直接使用 tornado.web.Application:

application = tornado.web.Application(endpoints, **settings)
application.listen(8888)
tornado.ioloop.IOLoop.current().start()

或通过 HTTPServer:

application = tornado.web.Application(endpoints, **settings)
server = tornado.httpserver.HTTPServer(application)
server.listen(8888)
tornado.ioloop.IOLoop.current().start()

有什么区别?文档还没有向我解释清楚。

来自the docs

Typical applications have little direct interaction with the HTTPServer class except to start a server at the beginning of the process (and even that is often done indirectly via tornado.web.Application.listen).

基本上,HTTPServer 是 Tornado 默认用于为其应用程序提供服务的 built-in HTTP 服务器实现。如果您需要另一个网络服务器处理得更好,您也可以通过不同的网络服务器(例如 via WSGI)提供 Tornado 应用程序。

Application.listen 是 shorthand 用于构建 HTTPServer 并在其上调用 listen。这很方便,因为您可能无论如何都需要导入 Application class 并且不需要触摸 HTTPServer.

您唯一需要手动构造 HTTPServer 的情况是您需要在其上调用 listen 以外的其他方法(您甚至可以传递 HTTPServer 构造函数参数通过 Application.listen,这样你就可以通过这种方式获得 HTTPS)。如果您使用 multi-process 模式,或者使用 HTTPServer.add_sockets 的一些不寻常的东西,您将需要这样做。