事件循环与多线程阻塞IO [英] Event Loop vs Multithread blocking IO

查看:148
本文介绍了事件循环与多线程阻塞IO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读关于服务器架构的评论。



http://news.ycombinator。 com / item?id = 520077



在此评论中,该人说3件事:


  1. 事件循环一次又一次显示出很多低活动连接的真正亮点。

  2. 相比之下,阻止IO模型与线程或进程一次又一次地显示出,与事件循环相比,可以根据每个请求减少延迟。

  3. 在轻载系统上,差异是无法区分的。在负载下,大多数事件循环选择减慢,大多数阻塞模型选择减轻负载。

这些都是真的吗? p>

另外还有一篇文章,题为为什么事件是一个坏主意(用于高并发服务器)



< a href =http://www.usenix.org/events/ photos03/tech/vonbehren.html =noreferrer> http://www.usenix.org/events/ photos03/tech/vonbehren.html

解决方案

通常,如果应用程序预计会处理数百万个连接,则可以将多线程范例与事件相结合基。


  1. 首先,生成N个线程,其中N ==机器上的核心/处理器数。每个线程将有一个它应该处理的异步套接字的列表。

  2. 然后,对于接收器的每个新连接,将新套接字负载平衡到最少的线程插座。

  3. 在每个线程中,对所有套接字使用基于事件的模型,以便每个线程实际上可以同时处理多个套接字。

使用这种方法,


  1. 你从未产生过百万个线程。您只需要与系统可以处理的一样多。

  2. 您可以使用基于多核的事件,而不是单个核心。


I was reading a comment about server architecture.

http://news.ycombinator.com/item?id=520077

In this comment, the person says 3 things:

  1. The event loop, time and again, has been shown to truly shine for a high number of low activity connections.
  2. In comparison, a blocking IO model with threads or processes has been shown, time and again, to cut down latency on a per-request basis compared to an event loop.
  3. On a lightly loaded system the difference is indistinguishable. Under load, most event loops choose to slow down, most blocking models choose to shed load.

Are any of these true?

And also another article here titled "Why Events Are A Bad Idea (for High-concurrency Servers)"

http://www.usenix.org/events/hotos03/tech/vonbehren.html

解决方案

Typically, if the application is expected to handle million of connections, you can combine multi-threaded paradigm with event-based.

  1. First, spawn as N threads where N == number of cores/processors on your machine. Each thread will have a list of asynchronous sockets that it's supposed to handle.
  2. Then, for each new connection from the acceptor, "load-balance" the new socket to the thread with the fewest socket.
  3. Within each thread, use event-based model for all the sockets, so that each thread can actually handle multiple sockets "simultaneously."

With this approach,

  1. You never spawn a million threads. You just have as many as as your system can handle.
  2. You utilize event-based on multicore as opposed to a single core.

这篇关于事件循环与多线程阻塞IO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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