繁体   English   中英

如何从自定义节点REPL获取子进程中的输入

[英]How to get input in child process from custom node REPL

我正在尝试创建一个节点repl,并希望产生一个子进程,该进程可以接受用户输入,然后将控制权返回给repl。

...主repl文件( a.js )的内容

!function(){
  var repl = require("repl"),
    spawn = require('child_process').spawn;

  function eval(cmd, context, filename, callback) {

    var child = spawn('node', ['./b.js']);

    child.stdout.pipe(process.stdout);
    child.stdin.pipe(process.stdin);

    child.on('close', function (code) {
      console.log('closing')
      callback(code, 'closing callback');
    });

  }

  repl.start({
    prompt: 'repl> ',
    input: process.stdin,
    output: process.stdout,
    eval: eval
  }).on('exit', function () {
    process.exit();
  });

}();

...在子进程( b.js )中调用的脚本的内容

!function(){

  promptly = require('promptly');

  promptly.prompt('some question: ', function (err, answer) {
    console.log(answer);
    process.exit();
  });

}();

当我运行node b一切都如预期...

$ node b
some question: something
something
$

但是从repl调用后,它进入循环,不断询问问题,再也没有返回repl ...

$ node a
repl> anything
some question: my answer
some question: another answer
some question: etc...

似乎stdin并未在子进程中被捕获,并且仍在repl中被捕获。

如何将控制权传递给子进程,直到完成,然后再传递回父进程?

nb我愿意使用任何其他方式来创建子进程。

a.js进行一些更改即可a.js这项工作。 您需要使用process.stdin.pipe(child.stdin); repl进程的stdin输入通过管道传递到子进程的stdin process.stdin.pipe(child.stdin);

为了获取数据需要从你到管子进程回到child.stdoutprocess.stdin使用child.stdout.pipe(process.stdout);

也许有更好的跟踪子进程的方法。 但是我添加了一个标记来标记是否生成了一个子代,并在子代进程关闭时将其重置。

最后,当子进程结束时,使用process.stdin.resume();恢复主进程stdin process.stdin.resume(); ,否则repl将在下一个输入时停止。

!function(){
  var repl = require("repl"),
    spawn = require('child_process').spawn;

  var child_spawned = false;
  function eval(cmd, context, filename, callback) {
    // don't start a new child process if one is already running
    if(child_spawned) return;

    var child = spawn('node', ['./b.js']);
    child_spawned = true;

    // pipe child output back up to parent process
    child.stdout.pipe(process.stdout);
    // pipe the main process input to the child process
    process.stdin.pipe(child.stdin);

    child.on('close', function (code) {
      console.log('closing')
      callback(code, 'closing callback');

      // mark child process as closed
      child_spawned = false;

      // resume the main process stdin after child ends so the repl continues to run
      process.stdin.resume();
    });

  }

  repl.start({
    prompt: 'repl> ',
    input: process.stdin,
    output: process.stdout,
    eval: eval
  }).on('exit', function () {
    process.exit();
  });

}();

我通过结合使用spawnSync (以简化流程)和{ stdio: 'inherit' }作为传递选项来解决此问题...

!function(){
  var repl = require("repl"),
    spawnSync = require('child_process').spawnSync;

  function eval(cmd, context, filename, callback) {
    spawnSync('node', ['./b.js'], { stdio: 'inherit' });
    callback();
  }

  repl.start({
    prompt: 'repl> ',
    input: process.stdin,
    output: process.stdout,
    eval: eval
  }).on('exit', function () {
    process.exit();
  });

}();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM