如何将 node.js 模块作为 node.js 程序的子进程执行?

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

How can I execute a node.js module as a child process of a node.js program?

node.jsnonblocking

提问by user1822364

Here's my problem. I implemented a small script that does some heavy calculation, as a node.js module. So, if I type "node myModule.js", it calculates for a second, then returns a value. Now, I want to use that module from my main Node.JS program. I could just put all the calculation in a "doSomeCalculation" function then do:

这是我的问题。我实现了一个小脚本,它进行一些繁重的计算,作为 node.js 模块。所以,如果我输入“node myModule.js”,它会计算一秒钟,然后返回一个值。现在,我想从我的主要 Node.JS 程序中使用该模块。我可以将所有计算放在“doSomeCalculation”函数中,然后执行以下操作:

var myModule = require("./myModule");
myModule.doSomeCalculation();

But that would be blocking, thus it'd be bad. I'd like to use it in a non-blocking way, like DB calls natively are, for instance. So I tried to use child_process.spawn and exec, like this:

但这会阻塞,因此会很糟糕。我想以非阻塞方式使用它,例如本地的 DB 调用。所以我尝试使用 child_process.spawn 和 exec,如下所示:

var spawn = require("child_process").spawn;
var ext = spawn("node ./myModule.js", function(err, stdout, stderr) { /* whatevs */ });
ext.on("exit", function() { console.log("calculation over!"); });

But, of course, it doesn't work. I tried to use an EventEmitter in myModule, emitting "calculationDone" events and trying to add the associated listener on the "ext" variable in the example above. Still doesn't work.

但是,当然,它不起作用。我尝试在 myModule 中使用 EventEmitter,发出“calculationDone”事件并尝试在上面的示例中的“ext”变量上添加关联的侦听器。还是不行。

As for forks, they're not really what I'm trying to do. Forks would require putting the calculation-related code in the main program, forking, calculating in the child while the parent does whatever it does, and then how would I return the result?

至于叉子,它们并不是我真正想要做的。Forks 需要将计算相关的代码放在主程序中,fork,在子程序中进行计算,而父级做任何事情,然后我将如何返回结果?

So here's my question: can I use a child process to do some non-blocking calculation, when the calculation is put in a Node file, or is it just impossible? Should I do the heavy calculation in a Python script instead? In both cases, how can I pass arguments to the child process - for instance, an image?

所以这是我的问题:当计算被放入节点文件时,我可以使用子进程进行一些非阻塞计算,还是不可能?我应该在 Python 脚本中进行繁重的计算吗?在这两种情况下,如何将参数传递给子进程 - 例如,图像?

回答by broofa

I think what you're after is the child_process.fork()API.

我认为你所追求的是child_process.fork()API。

For example, if you have the following two files:

例如,如果您有以下两个文件:

In main.js:

在 main.js 中:

var cp = require('child_process');
var child = cp.fork('./worker');

child.on('message', function(m) {
  // Receive results from child process
  console.log('received: ' + m);
});

// Send child process some work
child.send('Please up-case this string');

In worker.js:

在 worker.js 中:

process.on('message', function(m) {
  // Do work  (in this case just up-case the string
  m = m.toUpperCase();

  // Pass results back to parent process
  process.send(m.toUpperCase(m));
});

Then to run main (and spawn a child worker process for the worker.js code ...)

然后运行 ​​main (并为 worker.js 代码生成一个子工作进程......)

$ node --version
v0.8.3

$ node main.js 
received: PLEASE UP-CASE THIS STRING

回答by Aleksei Zabrodskii

It doesn't matter what you will use as a child (Node, Python, whatever), Node doesn't care. Just make sure, that your calculcation script exits aftereverything is done and result is written to stdout.

不管你小时候使用什么(Node、Python 等等),Node 都不在乎。只需确保您的计算脚本完成所有操作并将结果写入stdout.

Reason why it's not working is that you're using spawninstead of exec.

它不起作用的原因是您正在使用spawn而不是exec.