了解Ruby和OS I / O缓冲 [英] Understanding Ruby and OS I/O buffering

查看:205
本文介绍了了解Ruby和OS I / O缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IO缓存如何在Ruby中工作?使用 IO 文件类时,数据刷新到基础流的频率是多少?这与OS缓冲相比如何?需要做些什么来保证给定的数据写入磁盘,然后自信地将其读回处理?

How does IO buffering work in Ruby? How often is data flushed to the underlying stream when using the IO and File classes? How does this compare to OS buffering? What needs to be done to guarantee that given data has been written to disk, before confidently reading it back for processing?

推荐答案

Ruby IO文档并不是100%清楚这个缓冲是如何工作的,但是这是你可以从文档中提取的:

The Ruby IO documentation is not 100% clear on how this buffering works, but this is what you can extract from the documentation:


  • Ruby IO有它自己的内部缓冲区

  • 除此之外,底层操作系统可能会或可能不会进一步缓冲数据。

要查看的相关方法:


  • IO.flush :刷新 IO 。我还查看了Ruby源代码并调用 IO.flush 也调用底层操作系统 fflush() 。这应该足以让文件缓存,但不保证物理数据到磁盘。

  • IO.sync = :如果设置为 true ,则没有Ruby内部缓冲已经完成了。所有内容都会立即发送到操作系统,每次写入都会调用 fflush()

  • IO.sync :返回当前的同步设置( true false )。

  • IO.fsync :刷新Ruby缓冲区+调用 fsync() (如果它支持)。这将保证完全刷新到物理磁盘文件。

  • IO.close :关闭Ruby IO 并将待处理数据写入操作系统。请注意,这并不意味着 fsync()。有关 close() 的POSIX文档说它不保证数据物理写入文件。所以你需要使用一个明确的 fsync()调用。

  • IO.flush: Flushes IO. I also looked at the Ruby source and a call to IO.flush also calls the underlying OS fflush(). This should be enough to get the file cached, but does not guarantee physical data to disk.
  • IO.sync=: If set to true, no Ruby internal buffering is done. Everything is immidiately sent to the OS, and fflush() is called for each write.
  • IO.sync: Returns the current sync setting (true or false).
  • IO.fsync: Flushes both the Ruby buffers + calls fsync() on the OS (if it supports it). This will guarantee a full flush all the way to the physical disk file.
  • IO.close: Closes the Ruby IO and writes pending data to the OS. Note that this does not imply fsync(). The POSIX documentation on close() says that it does NOT guarantee data is physically written to the file. So you need to use an explicit fsync() call for that.

结论: flush 和/或 close 应足以让文件缓存,以便其他人可以完全读取过程或操作。要确保文件一直到达物理媒体,您需要调用 IO.fsync

Conclusion: flush and/or close should be enough to get the file cached so that it can be read fully by another process or operation. To get the file all the way to the physical media with certainty, you need to call IO.fsync.

其他相关方法:


  • IO.syswrite :绕过Ruby内部缓冲区并执行直接操作系统。如果你使用它,那么不要与 IO.read/write 混合。

  • IO.sysread :与上述相同,但需要阅读。

  • IO.syswrite: Bypass Ruby internal buffers and do a straight OS write. If you use this then do not mix it with IO.read/write.
  • IO.sysread: Same as above, but for reading.

这篇关于了解Ruby和OS I / O缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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