node.js 事件驱动和基于线程的服务器系统有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25280207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:45:43  来源:igfitidea点击:

What are the differences between event-driven and thread-based server system?

multithreadingnode.jsthreadpoolevent-driven

提问by erginduran

  • Node.jsis an event driven I/O and It's a single threaded server that acts upon callbacks and never blocks on the main thread.

    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?
  • Thread-basedmodel assigning a task to a thread and if there is no idle thread, block new tasks.

    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.
  • Node.js是一个事件驱动的 I/O,它是一个单线程服务器,它作用于回调并且从不阻塞主线程。

    1. 但是它是如何管理非阻塞 I/O 的呢?
    2. 如果它确实易于管理,那么基于线程的系统为什么不管理它呢?
    3. 其他线程(在单个事件驱动线程后面)不能像基于线程一样工作吗?
    4. 如果其他线程意味着工人(在事件驱动线程后面)很忙,它如何仍然可以在不阻塞的情况下处理作业?
  • 基于线程的模型将任务分配给线程,如果没有空闲线程,则阻止新任务。

    1. 如果一个线程可以处理多个任务,例如像事件驱动的单线程那样在不阻塞的情况下处理每个 I/O,那么为什么基于线程的系统不会在繁忙的线程上使用这种策略来进行 I/O 而不阻塞。

I am wondering what are the differences (advantages/disadvantages) between event-driven and thread-based server systems.

我想知道事件驱动和基于线程的服务器系统之间有什么区别(优点/缺点)。

回答by kamituel

The difference might be described as follows (with some simplification):

差异可以描述如下(有一些简化):

  • in "thread driven" runtimes, when a request comes in, a new thread is created and all the handling is done in that thread.

  • in "event driven" runtimes, when a request comes in, the event is dispatched and handler will pick it up. When? In Node.js, there is an "event loop" which basically loops over all the pieces of code that need to be executed and executes them one by one. So the handler will handle the event once event loop invokes it. The important thing here is that all the handlers are called in the same thread - the event loop doesn't have a thread pool to use, it only has one thread.

  • 在“线程驱动”运行时中,当请求进来时,会创建一个新线程,并且所有处理都在该线程中完成。

  • 在“事件驱动”运行时中,当请求进来时,事件被分派并且处理程序将接收它。什么时候?在 Node.js 中,有一个“事件循环”,它基本上会遍历所有需要执行的代码段并一一执行。因此,一旦事件循环调用它,处理程序就会处理该事件。这里重要的是所有处理程序都在同一个线程中调用——事件循环没有线程池可以使用,它只有一个线程。

In an "event driven" model, if a handler will take a very long time to finish (i.e. by having a computationally intensive forloop inside), no other request will be handled during that time, because the event loop will not invoke the next handler before the current one completes. That's usually not an issue because of the asynchronous nature of Javascript.

在“事件驱动”模型中,如果处理程序需要很长时间才能完成(即for内部具有计算密集型循环),则在此期间不会处理其他请求,因为事件循环不会调用下一个处理程序在当前完成之前。由于 Javascript 的异步特性,这通常不是问题。

On the other hand, in the "thread driven" model, if the handler takes a lot of time to complete, it won't hurt other threads much, because they can run at the same time independently.

另一方面,在“线程驱动”模型中,如果处理程序花费大量时间来完成,则不会对其他线程造成太大伤害,因为它们可以同时独立运行。

Unfortunately, creating a new thread adds some overhead and if you need to handle thousands of concurrent connections, it might become a burden. That's why Node.js is considered fast - no matter how many connections you handle, there's only one thread 1. You just need to be a bit careful not to block in any of the handlers to keep things moving. Fortunately most of the time it's not that easy to write blocking JavaScript code.

不幸的是,创建一个新线程会增加一些开销,如果您需要处理数千个并发连接,它可能会成为一种负担。这就是 Node.js 被认为是快速的原因 - 无论您处理多少个连接,都只有一个线程1。您只需要小心不要阻塞任何处理程序以保持移动。幸运的是,在大多数情况下,编写阻塞 JavaScript 代码并不容易。

It's also important to note that writing asynchronous code is possible in most of the runtimes. It has become most widely used in Node.js though, because of the nature of Javascript. Thanks to that, virtually every library you use in Node will be asynchronous.

同样重要的是要注意,在大多数运行时中都可以编写异步代码。不过,由于 Javascript 的性质,它已经在 Node.js 中得到了最广泛的使用。多亏了这一点,您在 Node 中使用的几乎每个库都将是异步的。

See this article (and pictures)for an explanation of the event loop.

有关事件循环的解释,请参阅本文(和图片)

1Of course there is more that one thread in Node.js process, some of them are related to I/O. But your app logic is handled in one thread.

1当然,Node.js 进程中还有不止一个线程,其中一些是与 I/O 相关的。但是您的应用程序逻辑是在一个线程中处理的。