[英]NodeJS child_process stdout, if process is waiting for stdin
[英]The stdout in child_process cannot output right row before stdin when exec shell in child_process
prompt.js:
var inquirer = require("inquirer");
console.log("Hi, welcome to Node Pizza");
var questions = [{
type: "confirm",
name: "toBeDelivered",
message: "Is it for a delivery",
default: false
}];
inquirer.prompt( questions, function( answers ) {
console.log("\nOrder receipt:");
console.log( JSON.stringify(answers, null, " ") );
});
index.js:
var childProcess = require('child_process');
var cp = childProcess.exec('node prompt.js', function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
if (err) {
console.log('exec error: ' + error);
}
});
process.stdin.pipe(cp.stdin);
在Windows7 x64 git shell(节点v0.10.26)中执行命令:
node index.js
您会看到,shell没有输出任何内容,正在等待读取输入。
为什么在读取输入之前没有输出“ Hi,Welcome to Node Pizza”之类的输出?
您应该使用child_process.spawn()而不是child_process.exec(),因为exec()缓冲输出并等待直到进程退出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.