最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Child_process throw an Error: write EPIPE - Stack Overflow

programmeradmin1浏览0评论

I just practise some node js code about child_process @the link My node version is V5.2.0 on windows 7.

// master.js
var cp=require("child_process");

var np=cp.fork("./console.js");               // line B

// block C
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");
});

// end of block C



np.send({Hello:"world"});
np.on("message",function(msg){
    console.log("parent process got a msg:",msg);
});




// console.js

#!/usr/bin/env node

var aa=1;
console.log("The aa is :"+aa);


process.on("message", function (m) {
    console.log("child process got message:",m);
});

process.send({foo:"bar"});

1) I run above code ,I got the error: write EPIPE. I googled and I don't find any useful answer. I just a new to nodejs, I follow the official doc and do a little modification, then the sample code failes. I find that if I ment out the code in block C, the sample code is ok. So I wonder why the code throw error, if np.stdout/np.stderr listens to 'data'?

$ The aa is :1
events.js:142
      throw er; // Unhandled 'error' event
      ^

Error: write EPIPE
    at exports._errnoException (util.js:856:11)
    at process.target._send (internal/child_process.js:607:18)
    at process.target.send (internal/child_process.js:508:19)
    at Object.<anonymous> (D:\Practice\..\console.js:
11:9)
    at Module._pile (module.js:399:26)
    at Object.Module._extensions..js (module.js:406:10)
    at Module.load (module.js:345:32)
    at Function.Module._load (module.js:302:12)
    at Function.Module.runMain (module.js:431:10)
    at startup (node.js:141:18)

2) I modify the master.js code, run the code like below:

var cp=require("child_process");

var np=cp.spawn("node", ["./console.js"]);

np.send({Hello:"world"});
np.on("message",function(msg){
    console.log("parent process got a msg:",msg);
});

It throws an error:

np.send({Hello:"world"});
   ^

TypeError: np.send is not a function

I revisit the node js official doc, I don't find the spawn() and fork() are very different. So I wonder why np.send is not a function?

Are there some points the official doc doesn't mention?

I just practise some node js code about child_process @the link My node version is V5.2.0 on windows 7.

// master.js
var cp=require("child_process");

var np=cp.fork("./console.js");               // line B

// block C
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");
});

// end of block C



np.send({Hello:"world"});
np.on("message",function(msg){
    console.log("parent process got a msg:",msg);
});




// console.js

#!/usr/bin/env node

var aa=1;
console.log("The aa is :"+aa);


process.on("message", function (m) {
    console.log("child process got message:",m);
});

process.send({foo:"bar"});

1) I run above code ,I got the error: write EPIPE. I googled and I don't find any useful answer. I just a new to nodejs, I follow the official doc and do a little modification, then the sample code failes. I find that if I ment out the code in block C, the sample code is ok. So I wonder why the code throw error, if np.stdout/np.stderr listens to 'data'?

$ The aa is :1
events.js:142
      throw er; // Unhandled 'error' event
      ^

Error: write EPIPE
    at exports._errnoException (util.js:856:11)
    at process.target._send (internal/child_process.js:607:18)
    at process.target.send (internal/child_process.js:508:19)
    at Object.<anonymous> (D:\Practice\..\console.js:
11:9)
    at Module._pile (module.js:399:26)
    at Object.Module._extensions..js (module.js:406:10)
    at Module.load (module.js:345:32)
    at Function.Module._load (module.js:302:12)
    at Function.Module.runMain (module.js:431:10)
    at startup (node.js:141:18)

2) I modify the master.js code, run the code like below:

var cp=require("child_process");

var np=cp.spawn("node", ["./console.js"]);

np.send({Hello:"world"});
np.on("message",function(msg){
    console.log("parent process got a msg:",msg);
});

It throws an error:

np.send({Hello:"world"});
   ^

TypeError: np.send is not a function

I revisit the node js official doc, I don't find the spawn() and fork() are very different. So I wonder why np.send is not a function?

Are there some points the official doc doesn't mention?

Share Improve this question edited May 10, 2016 at 12:53 koninos 5,3575 gold badges32 silver badges50 bronze badges asked May 10, 2016 at 12:05 gfangfan 1,0991 gold badge15 silver badges33 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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.js
var 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.js
var 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 node

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

发布评论

评论列表(0)

  1. 暂无评论