了解Ruby和OS I / O缓冲 [英] Understanding Ruby and OS I/O buffering
问题描述
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
:关闭RubyIO
并将待处理数据写入操作系统。请注意,这并不意味着fsync()
。有关close()
的POSIX文档说它不保证数据物理写入文件。所以你需要使用一个明确的fsync()
调用。
IO.flush
: FlushesIO
. I also looked at the Ruby source and a call toIO.flush
also calls the underlying OSfflush()
. This should be enough to get the file cached, but does not guarantee physical data to disk.IO.sync=
: If set totrue
, no Ruby internal buffering is done. Everything is immidiately sent to the OS, andfflush()
is called for each write.IO.sync
: Returns the current sync setting (true
orfalse
).IO.fsync
: Flushes both the Ruby buffers + callsfsync()
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 RubyIO
and writes pending data to the OS. Note that this does not implyfsync()
. The POSIX documentation onclose()
says that it does NOT guarantee data is physically written to the file. So you need to use an explicitfsync()
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 OSwrite
. If you use this then do not mix it withIO.read/write
.IO.sysread
: Same as above, but for reading.
这篇关于了解Ruby和OS I / O缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!