Node.js 中写入流上的多次写入
我一直在查看节点脏的代码,注意到当将大量数据写入文件时,原始程序员选择将写入分成几个组,并一次发出一个组的写入,但是他们全部作为一个循环的一部分同时发出,无需等待任何回调。对此我有三个问题。我有类似的问题需要解决。
- 这在某种程度上更有效吗?我也应该捆绑写入吗?
- 我应该如何选择最佳的捆绑尺寸?为什么不只写一组呢?
- 如果我在写入流上注册 on('drain') 事件,那么在所有同时发出的写入完成后,该事件是否只会发出一次?还是每次之后? (我的猜测是前者)
- 如果 on('error') 被发出,('drain') 事件也会被发出吗?或者它们是相互排斥的?
谢谢
I've been looking at the code of node-dirty and noticed that when writing a lot of data to a file, the original programmer has chosen to bunch the writes into several groups and issue writes of the groups one at a time, but they are all issued simultaneously as part of one loop, without waiting for any callbacks. I have three questions about this. I have a similar problem to solve.
- Is this more efficient in some way? Should I be bundling writes too?
- How should I choose the optimum bundle size? Why not just write one group?
- If I sign up to the on('drain') event on the writestream, will it be emitted only once after all the simultaneously issued writes have completed? Or after each? (my guess is the former)
- If the on('error') is emitted, will the ('drain') event also be emitted? Or are they mutually exclusive?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
进行许多小写入是低效的。发送写命令会产生开销。因此只写入 5 个字节而不是 1000 个字节会更昂贵。
最佳尺寸对我来说听起来像是一门黑魔法。我认为不把它写成一篇大文章是有充分理由的。可能要早一点开始写。早一点开始效率会更高一些。
当写入队列中的所有内容都完成写入时,Drain 会触发。因此,只要您追加到写入队列的速度比写入队列的速度快,它就应该只触发一次。你需要一个非常好的系统来处理这样的边缘情况。
即使它被发出,在“drain”中进行错误处理也是没有意义的。如果发生错误,我总是假设整个写入操作已失败,并且不会尝试在写入过程中恢复。
It's inefficient to make many small writes. sending a write command has overhead attached to it. So writing just 5 bytes instead of a 1000 is more expensive.
Optimum size sounds like a black art to me. I presume there are good reasons for not making it one big write. Probably to start writing earlier then later. It's slightly more efficient to start a bit earlier.
Drain triggers when everything in the write queue has finished writing. So as long as you append to the write queue faster then it writes it, it should only trigger once. You'd need one hell of a system to pull of an edge-case like that.
Even if it is emitted it doesn't make sense to do error handling in 'drain'. If an error has occurred I would always assume that the entire writing operation has failed and not try to recover mid-write.
您担心它,因为您不想在应用程序中正确维护状态。所以,也许你可以使用一个方便的函数:
现在,你可以将“error”处理程序设置为 vetoer,将“drain”处理程序设置为“proc”,而不用担心在调用“error”之后调用“drain”。
You are worried about it since you don't want to maintain state in your application right. So, maybe you could use a convenience function:
Now, you can set the 'error' handler to vetoer and the 'drain' handler to 'proc' and not wirry about 'drain' being called after 'error' is called.