相关疑难解决方法(0)

以流动模式流式传输子进程输出

我有使用Python编写的自定义命令行,它使用"print"语句打印其输出.我通过生成子进程并使用child.stdin.write方法向它发送命令从Node.js中使用它.这是来源:

var childProcess = require('child_process'),
    spawn = childProcess.spawn;

var child = spawn('./custom_cli', ['argument_1', 'argument_2']);

child.stdout.on('data', function (d) {
  console.log('out: ' + d);
});

child.stderr.on('data', function (d) {
  console.log('err: ' + d);
});

//execute first command after 1sec
setTimeout(function () {
  child.stdin.write('some_command' + '\n');
}, 1000);

//execute "quit" command after 2sec
//to terminate the command line
setTimeout(function () {
  child.stdin.write('quit' + '\n');
}, 2000);
Run Code Online (Sandbox Code Playgroud)

现在问题是我没有在流动模式下接收输出.我希望在打印后立即从子进程获取输出,但只有在子进程终止时才会收到所有命令的输出(使用自定义cli的quit命令).

javascript python node.js

10
推荐指数
2
解决办法
3377
查看次数

标签 统计

javascript ×1

node.js ×1

python ×1