关于Node.js中CPU密集型代码的困惑

时间:2022-02-10 15:13:16

A question regarding "everything runs in parallel except your code" from someone new to Node.js. This is an obviously artificial example, but let's say I want to create a math library containing a function factorize() which behaves as follows:

关于“除了你的代码之外的所有东西并行运行”的问题,来自Node.js的新人。这是一个明显人为的例子,但是我想说我想创建一个包含函数factorize()的数学库,其行为如下:

var http = require('http');
http.createServer(function (req, res) {
  myMath.factorize(some_big_number,function(factors) {
    res.writeHead(200, {'Content-Type': 'application/json'});
    res.end(JSON.stringify(factors));
  }
}).listen(8000);

How can this be written so that it will "run in parallel"?

如何编写它以便“并行运行”?

I've been looking at the parsing code from this library as an example that might take some processing time. Is the body of the code considered to be "your code", or does this "run in parallel"?

我一直在看这个库中的解析代码作为一个可能需要一些处理时间的例子。代码的主体被认为是“你的代码”,还是“并行运行”?

If not: What do I need to do when writing factorize() so that it is also non-blocking/behaves like a client? Is using EventEmitter sufficient?

如果不是:在写factorize()时我需要做什么,以便它也是非阻塞/表现得像客户端?使用EventEmitter是否足够?

If so: Is my best option still to use child processes as suggested in this question?

如果是这样的话:我仍然最好选择使用此问题中建议的子进程吗?

Apologies in advance for any lack of clarity.

如果不清楚,请提前道歉。

3 个解决方案

#1


7  

Actually you can't run it "parallel" (unless you use a workers module) as JavaScript in node.js is executed in single thread but you can split your single thread into smaller pieces. For example with process.nextTick, so when the CPU is executing the code as smaller chunks instead of one long running code, it has small breaks to run other things as well.

实际上你不能“并行”(除非你使用工人模块)运行它作为node.js中的JavaScript在单线程中执行,但你可以将你的单个线程分成更小的部分。例如,使用process.nextTick,因此当CPU以较小的块而不是一个长时间运行的代码执行代码时,它还有一些小的中断来运行其他东西。

myLongRunningCode(callback){
    do_a_piece_of_the_work();
    if(ready){
        callback();
    }else{
        // give the CPU a small break to do other things
        process.nextTick(function(){
            // continue working
            myLongRunningCode(callback);
        });
    }
}

#2


3  

To write non blocking code you have to do message passing.

要编写非阻塞代码,您必须进行消息传递。

To do message passing you have to open a stream and pass messages through it. This involves talking to some other process or talking to a sub process.

要进行消息传递,您必须打开一个流并通过它传递消息。这涉及与其他过程交谈或与子过程交谈。

You can create child processes to do heavy lifting for you in node or you can create a tcp/web service to do heavy lifting for you. Just get node to pass messages to them and then send data down your response when the external processes have done the heavy lifting.

您可以创建子进程以在节点中为您执行繁重的工作,或者您可以创建一个tcp / web服务来为您执行繁重的工作。只需让节点将消息传递给它们,然后在外部进程完成繁重工作时向下发送数据。

#3


1  

all your JS code can NOT run in parallel. There are never multiple functions executed at the same time. CPU intensive code would make your program unable to do something else until this code ends.

你的所有JS代码都不能并行运行。永远不会同时执行多个功能。在此代码结束之前,CPU密集型代码会使您的程序无法执行其他操作。

I recommend you to split your code with setTimeout or do your job in a separate process. But this must be REALLY intensive code ;)

我建议您使用setTimeout拆分代码,或者在单独的进程中完成工作。但这必须是真正密集的代码;)

#1


7  

Actually you can't run it "parallel" (unless you use a workers module) as JavaScript in node.js is executed in single thread but you can split your single thread into smaller pieces. For example with process.nextTick, so when the CPU is executing the code as smaller chunks instead of one long running code, it has small breaks to run other things as well.

实际上你不能“并行”(除非你使用工人模块)运行它作为node.js中的JavaScript在单线程中执行,但你可以将你的单个线程分成更小的部分。例如,使用process.nextTick,因此当CPU以较小的块而不是一个长时间运行的代码执行代码时,它还有一些小的中断来运行其他东西。

myLongRunningCode(callback){
    do_a_piece_of_the_work();
    if(ready){
        callback();
    }else{
        // give the CPU a small break to do other things
        process.nextTick(function(){
            // continue working
            myLongRunningCode(callback);
        });
    }
}

#2


3  

To write non blocking code you have to do message passing.

要编写非阻塞代码,您必须进行消息传递。

To do message passing you have to open a stream and pass messages through it. This involves talking to some other process or talking to a sub process.

要进行消息传递,您必须打开一个流并通过它传递消息。这涉及与其他过程交谈或与子过程交谈。

You can create child processes to do heavy lifting for you in node or you can create a tcp/web service to do heavy lifting for you. Just get node to pass messages to them and then send data down your response when the external processes have done the heavy lifting.

您可以创建子进程以在节点中为您执行繁重的工作,或者您可以创建一个tcp / web服务来为您执行繁重的工作。只需让节点将消息传递给它们,然后在外部进程完成繁重工作时向下发送数据。

#3


1  

all your JS code can NOT run in parallel. There are never multiple functions executed at the same time. CPU intensive code would make your program unable to do something else until this code ends.

你的所有JS代码都不能并行运行。永远不会同时执行多个功能。在此代码结束之前,CPU密集型代码会使您的程序无法执行其他操作。

I recommend you to split your code with setTimeout or do your job in a separate process. But this must be REALLY intensive code ;)

我建议您使用setTimeout拆分代码,或者在单独的进程中完成工作。但这必须是真正密集的代码;)