如何将node.js模块作为node.js程序的子进程执行? [英] How can I execute a node.js module as a child process of a node.js program?

查看:103
本文介绍了如何将node.js模块作为node.js程序的子进程执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题.我作为node.js模块实现了一个小的脚本,该脚本需要进行大量的计算.因此,如果我输入"node myModule.js",它将计算一秒钟,然后返回一个值. 现在,我想从主Node.JS程序中使用该模块.我可以将所有计算都放在"doSomeCalculation"函数中,然后执行:

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:

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

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

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:

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!"); });

但是,当然,它不起作用.我尝试在myModule中使用EventEmitter,发出"calculationDone"事件,并尝试在上面的示例中将关联的侦听器添加到"ext"变量上.仍然不起作用.

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.

至于叉子,它们并不是我真正想做的. Forks将需要将与计算相关的代码放入主程序中,进行派生,在父级执行任何操作时在子级中进行计算,然后如何返回结果?

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?

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

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?

推荐答案

我认为您追求的是

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

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

For example, if you have the following two files:

在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');

在worker.js中:

In 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));
});

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

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

$ node --version
v0.8.3

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

这篇关于如何将node.js模块作为node.js程序的子进程执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆