开发者

how do twisted/tornado et cetera work

I understand that they work in some way distinct from making a thread per u开发者_StackOverflowser. How exactly does that work?

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜