扭曲/龙卷风等如何工作

发布于 2024-09-05 19:39:45 字数 67 浏览 8 评论 0原文

我知道它们的工作方式与为每个用户创建一个线程不同。具体是如何运作的?

(“非阻塞”与此有什么关系吗?)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍暮颜 2024-09-12 19:39:45

来自扭曲文档:

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

另请参阅 http://en.wikipedia.org/wiki/Event_loop

非阻塞 涉及到,如果您想在单个线程中处理多个套接字上的事件(或者更一般地,来自两个以上任何类型的事件源),则不能使用阻塞< /em> 操作来处理这些事件。如果您在第一个套接字上执行阻塞读取,那么您将无法从第二个套接字读取数据,直到某些字节到达第一个套接字。这不太有效,因为您无法真正知道哪个套接字将首先读取字节。相反,您使用类似 select (在上面链接的维基百科页面上有更详细的描述)来告诉您哪个套接字有字节,然后从该套接字读取它们而不会阻塞。

这一切都意味着您可以为来自任意数量的事件源的事件提供服务,一个接一个,给人一种同时处理所有事件的感觉。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文