how do twisted/tornado et cetera work

被刻印的时光 ゝ 提交于 2019-12-10 13:57:14

问题


I understand that they work in some way distinct from making a thread per user. How exactly does that work?

(Does 'non-blocking' have something to do with it?)


回答1:


From the Twisted documentation:

The reactor is the core of the event loop within Twisted -- the loop which drives applications using Twisted. The event loop is a programming construct that waits for and dispatches events or messages in a program. It works by calling some internal or external "event provider", which generally blocks until an event has arrived, and then calls the relevant event handler ("dispatches the event"). The reactor provides basic interfaces to a number of services, including network communications, threading, and event dispatching.

See also http://en.wikipedia.org/wiki/Event_loop

Non-blocking relates in that if you want to handle events on more than one socket (or, more generally, from more than two of any kind of event source) in a single thread, you can't use blocking operations to handle those events. If you do a blocking read on the first socket, then you won't be able to read from the second socket until some bytes arrive on the first one. This doesn't work very well, since you can't really know which socket is going to have bytes to read first. Instead you use something like select (described in more detail on the Wikipedia page linked above) to tell you which socket has bytes and then read them from that socket without blocking.

This all means that you can service events from any number of event sources, one after another, giving the appearance of handling them all simultaneously.



来源:https://stackoverflow.com/questions/3067640/how-do-twisted-tornado-et-cetera-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!