获取使用继承创建的子进程的输出

时间:2017-11-23 00:59:59

标签: node.js child-process

我在节点中生成子进程,对于我正在使用的选项{stdio:'inherit'},我需要这样做,因为子进程需要接受来自用户的一些输入。

我还需要得到这个输出,因为孩子的输出实际上没什么,因为它正在使用父项。我考虑在孩子进程后快速process.stdout.on('data', ...)附加和分离,但我不想这样做。我假设这是一些节点流解决方案?喜欢首先拦截输入的东西,然后将它传递给我的父进程?

TLDR需要在节点中执行stdio:'inherit'时输出子进程。也许将子进程的输出传递给Buffer / string的东西?

谢谢,如果回答提供工作代码示例

,请

1 个答案:

答案 0 :(得分:0)

想出来!

const child_process = require('child_process');

const child = child_process.spawn('python3', ['./eater.py'], {
  stdio: ['inherit', 'pipe', 'pipe'],
});

const output = [];

child.stdout.on('data', d => {
  console.log(d.toString());
  output.push(d.toString());
});

child.stdout.on('end', () => {
  console.log('Finished');
  console.log({ output });
});

如果python类似于:

print("Some prompt") auth_code = input('Provide some code to us\n')

相关问题