你如何理解龙卷风中的ioloop?

Nic*_*.Xu 3 python tornado

我正在寻找一种方法来理解Tornado 中的 ioloop,因为我多次阅读官方文档,但无法理解。具体来说,它为什么存在。

from tornado.concurrent import Future
from tornado.httpclient import AsyncHTTPClient
from tornado.ioloop import IOLoop
def async_fetch_future():
    http_client = AsyncHTTPClient()
    future = Future()
    fetch_future = http_client.fetch(
        "http://mock.kite.com/text")
    fetch_future.add_done_callback(
        lambda f: future.set_result(f.result()))
    return future

response = IOLoop.current().run_sync(async_fetch_future) 
# why get current IO of this thread? display IO, hard drive IO, or network IO? 
print response.body
Run Code Online (Sandbox Code Playgroud)

我知道什么是IO,输入和输出,例如读取硬盘驱动器,在屏幕上显示图形,获取键盘输入。根据定义,IOLoop.current()返回此线程的当前 io 循环。

我的笔记本电脑上有很多 IO 设备运行这个 python 代码。这IOLoop.current()返回哪个 IO ?我从未听说过 javascript nodejs 中的 IO 循环。

此外,如果我只想做一个数据库查询,读取一个文件,我为什么要关心这个低级的事情?

Sra*_*raw 7

与其这么说IOLoop,也许EventLoop你更容易理解。

IOLoop.current()并没有真正返回 IO 设备,而只是一个纯 python 事件循环,它asyncio.get_event_loop()nodejs.

您需要事件循环来执行数据库查询的原因是您正在使用事件驱动结构来执行数据库查询(在您的示例中,您正在执行http请求)。

大多数时候你不需要关心这个低层结构。相反,您只需要使用async&await关键字。

假设有一个支持异步数据库访问的库:

async def get_user(user_id):
    user = await async_cursor.execute("select * from user where user_id = %s" % user_id)
    return user
Run Code Online (Sandbox Code Playgroud)

然后你只需要在你的处理程序中使用这个函数:

class YourHandler(tornado.web.RequestHandler):

    async def get():
        user = await get_user(self.get_cookie("user_id"))
        if user is None:
            return self.finish("No such user")
        return self.finish("Your are %s" % user.user_name)
Run Code Online (Sandbox Code Playgroud)


Ben*_*ell 6

我从未听说过 javascript nodejs 中的 IO 循环。

在 node.js 中,等效的概念是事件循环。节点事件循环大部分是不可见的,因为所有程序都使用它——它是在你的回调之间运行的。

在 Python 中,大多数程序不使用事件循环,所以当你想要一个时,你必须自己运行它。这可以是 Tornado IOLoop、Twisted Reactor 或 asyncio 事件循环(所有这些都是特定类型的事件循环)。

Tornado 的 IOLoop 的命名可能会令人困惑——它不直接执行任何 IO。相反,它协调程序中可能发生的所有不同的 IO(主要是网络 IO)。它可以帮助您将其视为“事件循环”或“回调运行程序”。