node.js 掌握Node JS替代多线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5200821/
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
Grasping the Node JS alternative to multithreading
提问by Bryan Field
If I understand correctly Node JS is non blocking...so instead of waiting for a response from a database or other process it moved on to something else and checks back later.
如果我理解正确,Node JS 是非阻塞的......所以它不会等待来自数据库或其他进程的响应,而是转移到其他东西并稍后再检查。
Also it is single threaded.
它也是单线程的。
So does all this mean that a given Node JS process can fully and efficiently utilize a single CPU core but it will not use any other core on the machine, as in, it will never use more than one at a time.
这一切是否意味着给定的 Node JS 进程可以完全有效地利用单个 CPU 内核,但它不会使用机器上的任何其他内核,因为它一次永远不会使用多个内核。
This of course means that the other CPUs can still be used by other processes for things like SQL database or other intentionally separated CPU heavy subroutines as long as they are a separate process.
这当然意味着其他 CPU 仍然可以被其他进程用于 SQL 数据库或其他有意分离的 CPU 繁重的子例程,只要它们是一个单独的进程。
Also in the event that the Node JS process has an endless loop or long running function, that process is no longer useful in any way until the endless loop or long running function is stopped (or whole process killed).
同样,如果 Node JS 进程具有无限循环或长时间运行的功能,则该进程在任何情况下都不再有用,直到无限循环或长时间运行的功能停止(或整个进程被终止)。
Is all this right? Am I correct in my understanding?
这一切都对吗?我的理解正确吗?
采纳答案by jcoder
Pretty much correct, yes. The node.js server has an internal thread pool so it can perform blocking operations and notify the main thread with a callback or event when things complete.
非常正确,是的。node.js 服务器有一个内部线程池,因此它可以执行阻塞操作并在事情完成时通过回调或事件通知主线程。
So I imagine that it will make limited use of another core for the thread pool, for example if you do a non-blocking file system read this is likely implemented by telling a thread from the thread pool to perform a read and set a callback when it's done which means that the read could be happening on a different thread/core while the main node.js program is doing something else.
所以我想它将有限地使用线程池的另一个核心,例如,如果您执行非阻塞文件系统读取,这可能是通过告诉线程池中的线程执行读取并设置回调来实现的它已经完成,这意味着读取可能发生在不同的线程/核心上,而主 node.js 程序正在做其他事情。
But from a node.js point of view, it's entirely single threaded and won't directly use more than one core.
但是从 node.js 的角度来看,它完全是单线程的,不会直接使用多个内核。
回答by Michael Borgwardt
Yes, I'd say that your understanding is entirely correct. This article(archived) explains the rationale behind this design quite well. This is probably the most important paragraph:
是的,我会说你的理解是完全正确的。这篇文章(存档)很好地解释了这种设计背后的基本原理。这可能是最重要的一段:
Apache is multithreaded: it spawns a thread per request (or process, it depends on the conf). You can see how that overhead eats up memory as the number of concurrent connections increases and more threads are needed to serve multiple simulataneous clients. Nginx and Node.js are not multithreaded, because threads and processes carry a heavy memory cost. They are single-threaded, but event-based. This eliminates the overhead created by thousands of threads/processes by handling many connections in a single thread.
Apache 是多线程的:它为每个请求(或进程,这取决于 conf)生成一个线程。您可以看到随着并发连接数量的增加以及需要更多线程来为多个并发客户端提供服务,这些开销是如何消耗内存的。Nginx 和 Node.js 不是多线程的,因为线程和进程的内存开销很大。它们是单线程的,但基于事件。通过在单个线程中处理多个连接,这消除了由数千个线程/进程创建的开销。
回答by infografnet
Even if this is an old thread, I thought, that I would share with an idea, how to utilize more that one core in Node.JS app. As Nuray Altin have mentioned - JXcorecan do that.
即使这是一个旧线程,我想,我也会分享一个想法,如何在 Node.JS 应用程序中使用更多的核心。正如 Nuray Altin 所提到的 - JXcore可以做到这一点。
Simple example:
简单的例子:
var method = function () {
console.log("this is message from thread no", process.threadId);
};
jxcore.tasks.runOnThread(0, method);
jxcore.tasks.runOnThread(1, method);
// this is message from thread no 1
// this is message from thread no 0
By default there are two threads (you can change it with jxcore.tasks.setThreadCount())
默认情况下有两个线程(您可以使用 更改它jxcore.tasks.setThreadCount())
Of course there is much more that you can do with tasks. The docs are here.
当然,您可以用任务做更多的事情。文档在这里。
Few articles on that subject:
关于该主题的文章很少:
回答by Nuray Altin
Since this question asked almost 2 years ago. Things are getting different or there are alternative approaches to multithreading problem on Node.JS
由于这个问题是在大约 2 年前提出的。事情变得不同了,或者有其他方法可以解决 Node.JS 上的多线程问题
According to below blog post, using the incoming 'task' extension, some can benefit from other available cores directly.
根据下面的博客文章,使用传入的“任务”扩展,一些可以直接从其他可用的内核中受益。
http://oguzbastemur.blogspot.com/2013/12/multithread-nodejs.html
http://oguzbastemur.blogspot.com/2013/12/multithread-nodejs.html
回答by Saurabh Lende
Node.js is a single-threaded application, but it can support concurrency via the concept of event and callbacks. Here is video by Philip Robertswhich explains how the event-loops works in javascript.
Node.js 是一个单线程应用程序,但它可以通过事件和回调的概念来支持并发。这是Philip Roberts 的视频,它解释了事件循环如何在 javascript 中工作。
(Instead of WebAPIs there are C++ APIs in Node.js)
(Node.js 中有 C++ API,而不是 WebAPI)

