child_process.spawn 上的 node.js EPIPE 异常
我正在使用 node.js v0.6.10
,尽管我在 0.6.7
上遇到了同样的问题。基本上,我使用 spawn
运行一个子进程,该子进程启动另一个 node.js 进程,并通过 stdout
和 stdin
进行通信,这是两个脚本:
< strong>parent (cli.js
):
var spawn = require("child_process").spawn;
var doSpawn = function(callback){
var child = spawn('child.js');
child.on('exit', function(code){
console.log("Child exited with code " + code);
});
child.stdin.write("ping");
child.stdin.end();
};
doSpawn();
setTimeout(function(){}, 10000);
child.js
var run = function(){
process.stdout.on('drain', function(){
process.exit(0);
});
process.stdout.write(stdout);
};
var stdin = process.stdin;
stdin.resume();
stdin.setEncoding("utf8");
var stdout = '';
stdin.on('data', function(data){
stdout += data;
});
stdin.on('end', run);
然后当我运行 node cli.js< /代码>:
$ node cli.js
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: write EPIPE
at errnoException (net.js:642:11)
at Object.afterWrite [as oncomplete] (net.js:480:18)
I'm using node.js v0.6.10
although I've got the same issue on 0.6.7
. Basically I run a child process using spawn
that starts another node.js process, and communicates over stdout
and stdin
Here are the two scripts:
parent (cli.js
):
var spawn = require("child_process").spawn;
var doSpawn = function(callback){
var child = spawn('child.js');
child.on('exit', function(code){
console.log("Child exited with code " + code);
});
child.stdin.write("ping");
child.stdin.end();
};
doSpawn();
setTimeout(function(){}, 10000);
child.js
var run = function(){
process.stdout.on('drain', function(){
process.exit(0);
});
process.stdout.write(stdout);
};
var stdin = process.stdin;
stdin.resume();
stdin.setEncoding("utf8");
var stdout = '';
stdin.on('data', function(data){
stdout += data;
});
stdin.on('end', run);
And then when I run node cli.js
:
$ node cli.js
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: write EPIPE
at errnoException (net.js:642:11)
at Object.afterWrite [as oncomplete] (net.js:480:18)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要运行另一个节点进程,建议使用 *child_process.fork()*
http://nodejs.org/docs/latest/api/child_processes.html #child_process.fork
更改后的代码:
To run another node process *child_process.fork()* is recommended
http://nodejs.org/docs/latest/api/child_processes.html#child_process.fork
Code with changes: