Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass messages as well as stdout from child to parent in node.js child process module?

I am having a problem with child-process module, specifically with child.spawn and child.fork. I am relying on the documentation of child_process.fork, which says:

This is a special case of the child_process.spawn() functionality for spawning Node.js processes. In addition to having all the methods in a normal ChildProcess instance, the returned object has a communication channel built-in. See child.send(message, [sendHandle]) for details.

I have simplified my problem below:

parent.js is:

var cp = require('child_process');
var n = cp.fork('./child.js');
n.send({a:1});
//n.stdout.on('data',function (data) {console.log(data);});
n.on('message', function(m) {
  console.log("Received object in parent:");
  console.log( m);
});

child.js is:

process.on('message', function(myObj) {
  console.log('myObj received in child:');
  console.log(myObj);
  myObj.a="Changed value";
  process.send(myObj);
});
process.stdout.write("Msg from child");

As expected. The output is:

Msg from child
myObj received in child:
{ a: 1 }
Received object in parent:
{ a: 'Changed value' }

I want it to work with the commented line in parent.js uncommented. In other words, I want to catch the stdout in the child process in the n.stdout.on('data'... statement in the parent process. If I uncomment it, I get an error:

n.stdout.on('data',function (data) {console.log(data);});
    ^
TypeError: Cannot read property 'on' of null

I do not mind using any of the child-process asynchronous variations, exec, fork or spawn. Any suggestions?

like image 762
Sunny Avatar asked Nov 02 '15 13:11

Sunny


People also ask

Does child process inherit stdout?

The stdin handle to the child process, if any, will be closed before waiting. This helps avoid deadlock: it ensures that the child does not block waiting for input from the parent, while the parent waits for the child to exit. By default, stdin, stdout and stderr are inherited from the parent.

What is a child_process module in node JS?

js allows single-threaded, non-blocking performance but running a single thread in a CPU cannot handle increasing workload hence the child_process module can be used to spawn child processes. The child processes communicate with each other using a built-in messaging system.


1 Answers

You need to set the silent property on the options object when you pass it in to fork() in order for the stdin, stdout and stderr to get piped back to the parent process.

e.g. var n = cp.fork('./child.js', [], { silent: true });

like image 161
Neil Avatar answered Oct 24 '22 17:10

Neil