Node.js stdout flush

时间:2011-10-31 13:27:49

标签: node.js

也许我正在复制this问题,但提供的答案可能是指较旧版本的节点(我使用的是v0.5.10),因为单独fs.writeSync(1, "Foo\n");无法解决我的问题(从shell脚本执行脚本时,输出不会被刷新)fs.fsyncSync(1);not documented,当我尝试它时,节点会引发异常:

 return binding.fsync(fd);
             ^
 Error: UNKNOWN, Operation complete;

at Object.fsyncSync (fs.js:353:18)
at Interface.<anonymous> (C:\Dev\Git\org.github.pistacchio.streamcommunication\nodebot.js:36:12)
at Interface.emit (events.js:67:17)
at Interface._onLine (readline.js:162:10)
at Interface._line (readline.js:426:8)
at Interface._ttyWrite (readline.js:603:14)
at ReadStream.<anonymous> (readline.js:82:12)
at ReadStream.emit (events.js:88:20)
at ReadStream._emitKey (tty.js:309:10)
at ReadStream.onData (tty.js:66:12)

1 个答案:

答案 0 :(得分:1)

你希望它被阻止,但我想streamlinejs也适合你:

var done = process.stdout.write(str);
if (!done) {
  // this effectively means "wait for this
  // event to fire", but it doesn't block everything
  process.stdout.on('drain', _)
}
doSome(otherStuff)

您可以粘贴此代码here,以查看调用它时将编译的内容。此外,您必须使用npm install streamline -g安装精简版,将您的代码放在名称以_.js结尾的文件中,并使用node-streamline filename_运行代码。