5

node.js v0.6.10で同じ問題が発生していますが、使用してい0.6.7ます。基本的に、spawn別のnode.jsプロセスを開始し、通信stdoutする子プロセスを実行しますstdin 。2つのスクリプトは次のとおりです。

親(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)
4

1 に答える 1

3

別のノードプロセスを実行するには、* 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);
于 2012-02-12T04:18:28.993 に答える