Eri*_*lam 5 spawn node.js epipe
我正在使用,node.js v0.6.10尽管我在0.6.7. 基本上我运行一个子进程使用spawn启动另一个node.js的过程,并通过连通stdout和stdin 这里有两个脚本:
父 ( 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);
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
然后当我运行时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)
Run Code Online (Sandbox Code Playgroud)
要运行另一个节点进程 *child_process.fork()* 建议 http://nodejs.org/docs/latest/api/child_processes.html#child_process.fork
更改后的代码:
var cp = require("child_process");
var doSpawn = function(callback){
var child = cp.fork('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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2716 次 |
| 最近记录: |