Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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(); 

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

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.

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?

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?

like image 501
user1822364 Avatar asked Nov 14 '12 00:11

user1822364


People also ask

Which module is required to create a child process?

Node provides child_process module which provides ways to create child process.

What is child process in node JS medium?

Node. js applications execute their code as a single Operating System process running on a single CPU meaning that it would work in a single-threaded mode, which can be ideal for light-weight applications.


1 Answers

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

For example, if you have the following two files:

In 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:

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 ...)

$ node --version v0.8.3  $ node main.js  received: PLEASE UP-CASE THIS STRING 
like image 152
broofa Avatar answered Sep 23 '22 17:09

broofa