扭曲/龙卷风等如何工作
我知道它们的工作方式与为每个用户创建一个线程不同。具体是如何运作的?
(“非阻塞”与此有什么关系吗?)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自扭曲文档:
另请参阅 http://en.wikipedia.org/wiki/Event_loop
非阻塞 涉及到,如果您想在单个线程中处理多个套接字上的事件(或者更一般地,来自两个以上任何类型的事件源),则不能使用阻塞< /em> 操作来处理这些事件。如果您在第一个套接字上执行阻塞读取,那么您将无法从第二个套接字读取数据,直到某些字节到达第一个套接字。这不太有效,因为您无法真正知道哪个套接字将首先读取字节。相反,您使用类似
select
(在上面链接的维基百科页面上有更详细的描述)来告诉您哪个套接字有字节,然后从该套接字读取它们而不会阻塞。这一切都意味着您可以为来自任意数量的事件源的事件提供服务,一个接一个,给人一种同时处理所有事件的感觉。
From the Twisted documentation:
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.