什么是“file.sync = true”做? [英] What does "file.sync = true" do?
问题描述
同步
用于下面的代码? fh = Tempfile.new('tmp')
fh.sync = true
它设置文件的同步模式。
这会影响将来的操作,导致输出写入时没有块缓冲。
如果 f.tty?
是真实的,也就是说,如果文件连接到类似控制台的设备,则输出不会被块缓冲。但是当输出到一个管道或文件时, f.tty?
将是错误的,I / O库将切换到块缓冲,是将输出累积在缓冲区中,只有在文件关闭,程序退出或缓冲区填满时才写入。这是更快,最终结果是相同的。
设置 f.sync = true
击败此开关。如果管道的输出连接到实际上是控制台的东西,或者以某种方式进行交互,或者如果文件的内容正在被主动监视的话,这可能是有用的。
I had a look at the docs but I can't seem to find the relevant part. Can anyone tell me what the call to sync
is for in the following code?
fh = Tempfile.new('tmp')
fh.sync = true
It sets the sync mode of the file.
This affects future operations and causes output to be written without block buffering.
If f.tty?
is true, that is, if the file is connected to a console-like device, then output is not block buffered. But when output goes to a pipe or file, f.tty?
will be false and the I/O library will switch to block buffering, that is, accumulating output in a buffer and writing it only if the file is closed, the program exits, or the buffer fills up. This is faster and the end result is the same.
Setting f.sync = true
defeats this switch. This can be useful if the output of the pipe is connected to something that actually is a console or in some way interactive or if the contents of the file are being actively monitored.
这篇关于什么是“file.sync = true”做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!