对 Tornado 中的 IOLoop 如何获取 Application 对象感到困惑
confused about how the IOLoop in Tornado picks up the Application object
Tornado 新手,正在尝试了解基础知识。给定 their sample application:
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
我知道应用程序对象有配置值和 RequestHandlers(基本上是应用程序行为),我也知道 IOLoop 是 python3 异步模块的某种包装器。查看 the documentation for asyncio 向我展示了这个示例:
import asyncio
import time
async def say_after(delay, what):
await asyncio.sleep(delay)
print(what)
async def main():
print(f"started at {time.strftime('%X')}")
await say_after(1, 'hello')
await say_after(2, 'world')
print(f"finished at {time.strftime('%X')}")
asyncio.run(main())
这里 asyncio
被明确告知 运行 函数 main()
而在龙卷风示例中我没有看到事件循环被告知 运行应用程序对象。两者如何关联?
已经 and here
不错!
Application.listen
最终调用 tornado.netutil.add_accept_handler
,它通过 IOLoop.current()
静态方法在当前 IOLoop 上注册。
Tornado 新手,正在尝试了解基础知识。给定 their sample application:
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
我知道应用程序对象有配置值和 RequestHandlers(基本上是应用程序行为),我也知道 IOLoop 是 python3 异步模块的某种包装器。查看 the documentation for asyncio 向我展示了这个示例:
import asyncio
import time
async def say_after(delay, what):
await asyncio.sleep(delay)
print(what)
async def main():
print(f"started at {time.strftime('%X')}")
await say_after(1, 'hello')
await say_after(2, 'world')
print(f"finished at {time.strftime('%X')}")
asyncio.run(main())
这里 asyncio
被明确告知 运行 函数 main()
而在龙卷风示例中我没有看到事件循环被告知 运行应用程序对象。两者如何关联?
已经
不错!
Application.listen
最终调用 tornado.netutil.add_accept_handler
,它通过 IOLoop.current()
静态方法在当前 IOLoop 上注册。