为什么 node.js 不适合重 CPU 应用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16974557/
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 14:43:45  来源:igfitidea点击:

Why is node.js not suitable for heavy CPU apps?

javascriptnode.js

提问by vuvu

Node.js servers are very efficient concerning I/O and large number of client connection. But why is node.js not suitable for heavy CPU apps in comparison to a traditional multithreading server?

Node.js 服务器在 I/O 和大量客户端连接方面非常高效。但是,与传统的多线程服务器相比,为什么 node.js 不适合大量 CPU 应用程序?

I read it here Felix Baumgarten

我在这里读到了Felix Baumgarten

回答by poke

Node is, despite its asynchronous event model, by nature single threaded. When you launch a Node process, you are running a single process with a single thread on a single core. So your codewill not be executed in parallel, only I/O operations are parallel because they are executed asynchronous. As such, long running CPU tasks will block the whole server and are usually a bad idea.

尽管 Node 是异步事件模型,但本质上是单线程的。当您启动 Node 进程时,您是在单个内核上运行具有单个线程的单个进程。所以你的代码不会并行执行,只有 I/O 操作是并行的,因为它们是异步执行的。因此,长时间运行的 CPU 任务会阻塞整个服务器,这通常是一个坏主意。

Given that you just start a Node process like that, it is possible to have multiple Node processes running in parallel though. That way you could still benefit from your multithreading architecture, although a single Node process does not. You would just need to have some load balancer in front that distributes requests along all your Node processes.

鉴于您只是像这样启动一个 Node 进程,但可能有多个 Node 进程并行运行。这样你仍然可以从你的多线程架构中受益,尽管单个 Node 进程不会。你只需要在前面有一些负载均衡器,它可以沿着你的所有 Node 进程分发请求。

Another option would be to have the CPU work in separate processes and make Node interact with those instead of doing the work itself.

另一种选择是让 CPU 在单独的进程中工作,并使 Node 与这些进程交互,而不是自己完成工作。

Related things to read:

相关阅读:

回答by Chuck

A simple Node.js server is single-threaded, meaning that any operation that takes a long time to execute will block the rest of your program from running. Node.js apps manage to maintain a high level of concurrency by working as a series of events. When an event handler is waiting for something to happen (such as reading from the database), it tells Node to go ahead and process another event in the meantime. But since a single thread can only execute one instruction at a time, this approach can't save you from a function that needs to keep actively executing for a long time. In a multithreaded architecture, even if one function takes a long time to compute the result, other threads can still process other requests — and as long as you have a core that is not fully used at the time, there's a good chance they can do it about as quickly as if no other requests were running at all.

一个简单的 Node.js 服务器是单线程的,这意味着任何需要很长时间执行的操作都会阻止程序的其余部分运行。Node.js 应用程序通过作为一系列事件工作来维持高水平的并发性。当事件处理程序正在等待发生某些事情时(例如从数据库中读取),它会告诉 Node 在此期间继续处理另一个事件。但是由于单个线程一次只能执行一条指令,因此这种方法无法使您免于需要长时间保持主动执行的功能。在多线程架构中,即使一个函数需要很长时间来计算结果,其他线程仍然可以处理其他请求——只要你有一个当时没有完全使用的核心,就会有

In order to deal with this, production Node.js apps that expect to hog a lot of CPU will usually be run in clusters. This means that instead of having several threads in one program's memory space, you run several instances of the same program under the control of one "master" instance. Each process is single-threaded, but since you have several of them, you end up gaining the benefits of multiple threads.

为了解决这个问题,预计会占用大量 CPU 的生产 Node.js 应用程序通常会在集群中运行。这意味着不是在一个程序的内存空间中有多个线程,而是在一个“主”实例的控制下运行同一程序的多个实例。每个进程都是单线程的,但是由于您有多个进程,您最终会获得多线程的好处。

回答by Bhanuprakash Reddy

Node is flawless if you are having asynchronous tasks because java script will run these things by worker pool. But if you run CPU intense tasks (where you heavily use CPU ) Ex you have a billion users and you want to sort those people on name. Its quit a Intense tasks, and this is synchronous which will block other code from running.

如果您有异步任务,Node 是完美的,因为 Java 脚本将通过工作池运行这些东西。但是,如果您运行 CPU 密集型任务(您大量使用 CPU 的地方)例如,您有十亿用户,并且您想按姓名对这些人进行排序。它退出了一个 Intense 任务,这是同步的,它将阻止其他代码运行。

So its not a good idea to use node for these kind of applications. Technically you can find alternatives to address those kind of tasks. The above example is better addressed in a Db. then passing that result is great.

因此,将节点用于此类应用程序并不是一个好主意。从技术上讲,您可以找到解决此类任务的替代方法。上面的例子在 Db 中更好地解决。然后通过那个结果是伟大的。

In the same way avoid Intense task and keep your CPU cool for better performance

以同样的方式避免 Intense 任务并保持 CPU 冷却以获得更好的性能

回答by Yao Zhao

You can have a look at this package, the-computer, which may help you do some cpu intensive works in a single instance of node.js app in a simple way.

您可以查看这个包the-computer,它可以帮助您以简单的方式在 node.js 应用程序的单个实例中完成一些 CPU 密集型工作。

Definitely it is not as effective as raw c++ libs, but it can cover most general computing cases, keeping you in node.js garden while allowing you leverage the cores of the cup.

肯定它不如原始 c++ 库有效,但它可以涵盖大多数通用计算情况,让您在 node.js 花园中保持活力,同时允许您利用杯子的核心。