扭曲/龙卷风等如何工作 [英] how do twisted/tornado et cetera work

查看:52
本文介绍了扭曲/龙卷风等如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道它们的工作方式与为每个用户创建一个线程不同.这究竟是如何工作的?

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?)

推荐答案

来自 Twisted 文档:

From the Twisted documentation:

反应器是 Twisted 中事件循环的核心——使用 Twisted 驱动应用程序的循环.事件循环是一种在程序中等待和分派事件或消息的编程结构.它通过调用一些内部或外部的事件提供者"来工作,它通常会阻塞直到事件到达,然后调用相关的事件处理程序(调度事件").反应器为许多服务提供基本接口,包括网络通信、线程和事件调度.

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.

另见http://en.wikipedia.org/wiki/Event_loop

非阻塞 涉及的是,如果您想在一个线程中处理多个套接字上的事件(或者,更一般地说,来自两个以上任何类型的事件源),您不能使用阻塞操作来处理这些事件.如果您在第一个套接字上进行阻塞读取,那么在第一个套接字到达某些字节之前,您将无法从第二个套接字读取.这不能很好地工作,因为您无法真正知道哪个套接字将首先读取字节.相反,您使用诸如 select 之类的东西(在上面链接的维基百科页面上有更详细的描述)来告诉您哪个套接字具有字节,然后从该套接字中读取它们而不会阻塞.

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.

这篇关于扭曲/龙卷风等如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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