Tornado ioloop 和 Tornado 的工作流程是什么? [英] what's the tornado ioloop, and tornado's workflow?

查看:38
本文介绍了Tornado ioloop 和 Tornado 的工作流程是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 tornado 的内部工作流程,并且看过这篇文章,这很棒,但我做不到弄清楚

i want to know tornado's internal workflow, and have seen this article, it's great, but something i just can't figure out

ioloop.py里面有这样一个函数

within the ioloop.py, there is such a function

def add_handler(self, fd, handler, events):
    """Registers the given handler to receive the given events for fd."""
    self._handlers[fd] = handler
    self._impl.register(fd, events | self.ERROR)

这是什么意思?每个请求都会触发 add_handler 还是在 init 时只触发一次?

so what's this mean? every request will trigger add_handler or it's just triggered once when init?

每个套接字连接都会生成一个文件描述符,还是只生成一次?

every socket connect will generate a file descriptor , or it's just generated once?

ioloop 和 iostream 有什么关系?

what's the relationship between ioloop and iostream ?

httpserver 如何与 ioloop 和 iostream 一起工作?

how does httpserver work with ioloop and iostream ?

有没有工作流程图,让我看得很清楚?

is there any workflow chart, so i can see it clearly ?

抱歉这些问题,我只是困惑

sorry for these questiones, i just confused

任何链接、建议、提示都有帮助.非常感谢:)

any link, suggestion, tip helps. many thanks :)

推荐答案

我看看能不能按顺序回答你的问题:

I'll see if I can answer your questions in order:

  • 这里的 _impl 是可用的套接字轮询机制,epoll 在 Linux 上,select 在 Windows 上.所以 self._impl.register(fd, events | self.ERROR) 将等待某个事件"请求传递给底层操作系统,也特别包括错误事件.

  • Here _impl is whichever socket polling mechanism is available, epoll on Linux, select on Windows. So self._impl.register(fd, events | self.ERROR) passes the "wait for some event" request to the underlying operating system, also specifically including error events.

运行时,HTTPServer 将注册套接字以接受连接,使用 IOLoop.add_handler().当连接被接受时,它们将生成更多的通信套接字,这些套接字可能还会通过 IOStream 添加事件处理程序,它也可能调用 add_handler().因此,将在开始时和接收连接时添加新的处理程序.

When run, the HTTPServer will register sockets to accept connections on, using IOLoop.add_handler(). When connections are accepted, they will generate more communication sockets, which will likely also add event handlers via an IOStream, which may also call add_handler(). So new handlers will be added both at the beginning, and as connections are recieved.

是的,每个新的套接字连接都会有一个唯一的文件描述符.HTTPServer 正在侦听的原始套接字应该保留其文件描述符.文件描述符由操作系统提供.

Yes, every new socket connection will have a unique file descriptor. The original socket the HTTPServer is listening on should keep its file descriptor though. File descriptors are provided by the operating system.

IOLoop 处理与套接字有关的事件,例如它们是否有可供读取的数据、它们是否可以写入以及是否发生了错误.通过使用诸如epollselect 之类的操作系统服务,它可以非常有效地做到这一点.

The IOLoop handles events to do with the sockets, for example whether they have data available to be read, whether they may be written to, and whether an error has occured. By using operating system services such as epoll or select, it can do this very efficiently.

IOStream 通过单个连接处理流数据,并使用 IOLoop 异步执行此操作.例如,IOStream 可以读取尽可能多的数据,然后使用 IOLoop.add_handler() 等待更多数据可用.

An IOStream handles streaming data over a single connection, and uses the IOLoop to do this asynchronously. For example an IOStream can read as much data as is available, then use IOLoop.add_handler() to wait until more data is available.

listen() 上,HTTPServer 创建一个套接字,它使用 IOLoop 来监听连接.当获得连接时,它使用 socket.accept() 创建一个新的套接字,然后使用新的 HTTPConnection 与客户端通信.

On listen(), the HTTPServer creates a socket which it uses to listen for connections using the IOLoop. When a connection is obtained, it uses socket.accept() to create a new socket which is then used for communicating with the client using a new HTTPConnection.

HTTPConnection 使用 IOStream 来向或从客户端传输数据.此 IOStream 使用 IOLoop 以异步和非阻塞的方式执行此操作.许多 IOStreamHTTPConnection 对象可以同时处于活动状态,所有对象都使用相同的 IOLoop.

The HTTPConnection uses an IOStream to transfer data to or from the client. This IOStream uses the IOLoop to do this in an asynchronous and non-blocking way. Many IOStream and HTTPConnection objects can be active at once, all using the same IOLoop.

我希望这能解答您的一些问题.我不知道一个好的结构图,但其他网络服务器的总体思路也应该非常相似,所以可能会有一些很好的信息.您链接到的那篇深入的文章确实看起来非常有用,因此如果您足够了解,我建议您再试一次:)

I hope this answers some of your questions. I don't know of a good structural chart, but the overall idea should be fairly similar for other webservers too, so there might be some good info around. That in-depth article you linked to did look pretty useful, so if you understand enough I'd recommend giving it another go :).

这篇关于Tornado ioloop 和 Tornado 的工作流程是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆