为什么Node.js等待从child_process.spawn发布STDOUTstream数据?

我刚开始着眼于在Electron中构build应用程序,所以如果我以不恰当的方式使用这个问题中的单词构造,我会提前道歉。

在学习的同时,我只是试图将从过去创build的IRC bot收集的所有STDOUT数据发送到应用程序中的div元素。

index.html

 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Bot World!</title> </head> <body> <h1>Bot World!</h1> <button type="button" id="bot_button">Run Bot</button> <p id="output"></p> </body> <script> document.getElementById('bot_button').addEventListener("click", function (e) { var spawn = require('child_process').spawn; var child = spawn('ruby' , ['/home/syncthetic/Projects/Box/bots/hackthissite']); child.stdout.on('data', function(data) { console.log('stdout: ' + String(data)); document.getElementById('bot_button').innerHTML += String(data) + '</b>'; }); child.stderr.on('data', function(data) { console.log('stdout: ' + String(data)); document.getElementById('bot_button').innerHTML += String(data) + '</b>'; }); child.on('exit', function(code) { console.log('stdout: ' + String(data)); document.getElementById('bot_button').innerHTML += String(data) + '</b>'; }); }) </script> </html> 

main.js

 const electron = require('electron') const app = electron.app const BrowserWindow = electron.BrowserWindow let mainWindow function createWindow () { mainWindow = new BrowserWindow({width: 800, height: 600}) mainWindow.loadURL(`file://${__dirname}/index.html`) mainWindow.webContents.openDevTools() mainWindow.on('closed', function () { mainWindow = null }) } app.on('ready', createWindow) app.on('window-all-closed', function () { if (process.platform !== 'darwin') { app.quit() } }) app.on('activate', function () { if (mainWindow === null) { createWindow() } }) 

package.json只是调用main.js文件

IRC Bot的来源: https : //github.com/syncthetic/Box/

看起来无论我连接什么networking, Electron应用程序都会在发布到console.log之前等待特定数量的字节并追加到output div

我读了Node的exec命令会让它读取数据到buffer ,而spawn应该允许你实时访问这些数据。

当程序本身将数据推送到STDOUT时,我怎样才能实时发送到应用程序?

为了logging,我也尝试使用execFile ,并没有按照我的意愿去做我想做的事情。

我设法find解决scheme,我自己的答案!

显然, RubyPython ,毫无疑问,其他应用程序/语言默认情况下会将数据发送到STDOUT缓冲区中。

特别是要解决与运行的Ruby脚本的问题,我不得不启用STDOUT syncing与下面的行STDOUT syncing

$stdout.sync = true