读取等待input的产生的进程的stdout
我有一个Swift程序,在等待\n
(终止执行)时将信息logging到stdout
。 开始运行后立即请求input,并在1〜2秒后logging信息:
fetchAndLogDataInBackground(); // will print some data in ~1 sec readLine();
我正在spawn
/ execFile
用以下方式进行文件编辑:
const spawn = require('child_process').spawn; const process = spawn('swift/main'); process.stdout.on('data', data => console.log(data.toString())); setTimeout(() => { process.stdin.write('\n'); }, 5000);
因为我正在使用on('data')
,所以我期望看到日志“live”,但是它们只是在process.stdin.write('\n');
之后才被处理process.stdin.write('\n');
有什么办法让数据“活”?
PS:如果我在terminal( swift/main
)中运行Swift程序,它可以正常工作。
您可能不得不禁用标准输出缓冲或手动清除标准input在Swift中。
手动刷新:
fflush(__stdoutp)
要禁用输出缓冲:
setbuf(__stdoutp, nil);