繁体   English   中英

当child_process中的exec shell时,child_process中的stdout无法在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.

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