如何用node.js在stdout中编写阻塞? [英] How can I write blocking in stdout with node.js?

查看:98
本文介绍了如何用node.js在stdout中编写阻塞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个node.js应用程序,它将stdout传送到一个文件。我正在使用console.log编写所有内容。一段时间后,我的应用程序达到1GB限制并停止。有趣的是,如果我使用console.error而不是console.log,内存使用率会保持较低并且程序运行正常。所以看起来node.js无法刷新stdout流,所有内容都保存在内存中。我想让stderr免费提供错误。

I'm writing a node.js application which stdout is piped to a file. I'm writing everything with console.log. After a while my Application reaches the 1GB Limit and stops. The interesting thing is, that if I use console.error instead of console.log, the memory usage keeps low and the programm runs fine. So it looks like node.js can't flush the stdout stream and everything is kept in memory. I wanna keep stderr free for errors.

我的问题是:

有没有办法将阻塞写入stdout?或者至少,我可以写一个回调到stdout,所以我可以确保我写的不是太多吗?

Is there a way to write blocking into stdout? Or at least, can I write with a callback to stdout, so I can ensure I'm writing not too much?

thx!

推荐答案

如果你真的想要同步写入stdout,你可以这样做:

If you really really want synchronous writes to stdout you can do:

var fs = require('fs');
fs.writeSync(1, "Foo\n");
fs.fsyncSync(1);

这篇关于如何用node.js在stdout中编写阻塞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆