1. But how does it manage to non-blocking I/O?
2. if it does easy to manage, why don't thread-based system manage it?
3. Does not work the other threads (behind single event-driven thread) as like thread-based ?
4. if the other threads mean workers(behind event driven thread) are busy, how it still can handle jobs without blocking?
1. if a thread can handle multiple tasks like as event-driven single thread that handles every I/O without blocking, why thread-based system doesn't use this tactic on busy threads to I/O without blocking.
我想知道事件驱动的服务器系统和基于线程的服务器系统有什么不同(优缺点)。
发布于 2014-08-13 07:24:47
这种区别可以描述如下(稍作简化):
在“事件驱动”模型中,如果一个处理程序需要很长时间才能完成(即在该模型中有一个计算密集型的for循环),则在此期间不会处理其他请求,因为事件循环不会在当前处理程序完成之前调用下一个处理程序。这通常不是一个问题,因为Javascript的异步性质。
另一方面,在“线程驱动”模型中,如果处理程序需要大量时间来完成,它不会对其他线程造成太大伤害,因为它们可以同时独立运行。
不幸的是,创建一个新线程会增加一些开销,如果您需要处理数千个并发连接,它可能会成为一个负担。这就是为什么Node.js被认为是快速的--不管您处理了多少个连接,只有一个线程1。您只需要稍微小心一点,不要阻塞任何处理程序,以保持事物的正常运行。幸运的是,大多数时候编写阻塞JavaScript代码并不容易。
同样重要的是要注意,在大多数运行时编写异步代码是可能的。但是,由于Javascript的特性,它在Node.js中的应用最为广泛。因此,您在Node中使用的几乎每个库都将是异步的。
有关事件循环的解释,请参见这篇文章(和图片)。
1当然有更多的Node.js进程中的线程,其中有些与I/O相关,但是应用程序逻辑是在一个线程中处理的。
https://stackoverflow.com/questions/25280207
复制相似问题