Child_process Throw An Error: Write Epipe
Solution 1:
1/ About np.send not found method
child_process.send method is available when, the child is forked, or when its pipe are set to IPC
fork
When using child_process.fork() you can write to the child using child.send(message[, sendHandle][, callback]) and messages are received by a 'message' event on the child.
ipc
'ipc' - Create an IPC channel for passing messages/file descriptors between parent and child. A ChildProcess may have at most one IPC stdio file descriptor. Setting this option enables the ChildProcess.send() method. If the child writes JSON messages to this file descriptor, then this will trigger ChildProcess.on('message'). If the child is an Node.js program, then the presence of an IPC channel will enable process.send() and process.on('message').
var child = spawn('cat', ['index.js', {stdio: 'ipc'});
2/ About EPIPE
EPIPE means you're writing to a pipe or socket when the other end has terminated the connection.
This said, i found your observations suspicious because if you don t open the pipes, stdout && stderr && stdin objects of the child won t be populated. Thus it usually rise an error such TypeError: Cannot read property 'on' of null
.
Then i double check the doc and seen that fork are very special, and i noticed that they don t provide any stdio
option. But, it is said that it enables send
method.
My understanding is that fork method won t open any pipe and that only send
method, on('message')
mechanism are available.
This code works, with console.js
left untouched
// master.jsvar cp=require("child_process");
var np = cp.fork("./console.js");
np.on("close", function () {
console.log("child process exit");
});
np.send({Hello:"world"});
np.on("message",function(msg){
console.log("parent process got a msg:",msg);
});
To do something similar, but with spawn, we should take advantage of stdin to write, stdout or stderr to listen,
// master.jsvar cp=require("child_process");
var np = cp.spawn(process.argv[0], ["./console.js"], {stdio: 'pipe'});
np.stdout.on("data",function(data){
console.log("child process output:"+data);
});
np.stderr.on("data", function(err){
console.log("child process output error:"+err);
});
np.on("close", function () {
console.log("child process exit");
});
np.stdin.end(JSON.stringify({Hello:"world"}))
console.js
#!/usr/bin/env nodevar aa=1;
console.log("The aa is :"+aa);
process.stdin.on("data", function (m) {
console.log("child process got message:", m.toString());
});
process.stdout.write(JSON.stringify({foo:"bar"}))
// process.send({foo:"bar"});
That s it. I m not enable to give you precise answer about the specific error you provided.
I hope this helps.
Post a Comment for "Child_process Throw An Error: Write Epipe"