file.flush() 到底在做什么? [英] What exactly is file.flush() doing?

查看:21
本文介绍了file.flush() 到底在做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Python 文件对象文档:

I found this in the Python documentation for File Objects:

flush() 不一定会将文件的数据写入磁盘.使用 flush() 后跟 os.fsync() 以确保这种行为.

flush() does not necessarily write the file’s data to disk. Use flush() followed by os.fsync() to ensure this behavior.

所以我的问题是:Python 的 flush 到底在做什么?我认为它会强制将数据写入磁盘,但现在我发现它没有.为什么?

So my question is: what exactly is Python's flush doing? I thought that it forces to write data to the disk, but now I see that it doesn't. Why?

推荐答案

通常涉及两个级别的缓冲:

There's typically two levels of buffering involved:

  1. 内部缓冲区
  2. 操作系统缓冲区

内部缓冲区是由您正在编程的运行时/库/语言创建的缓冲区,旨在通过避免每次写入的系统调用来加快速度.相反,当您写入文件对象时,您会写入其缓冲区,并且每当缓冲区填满时,数据就会使用系统调用写入实际文件.

The internal buffers are buffers created by the runtime/library/language that you're programming against and is meant to speed things up by avoiding system calls for every write. Instead, when you write to a file object, you write into its buffer, and whenever the buffer fills up, the data is written to the actual file using system calls.

但是,由于操作系统缓冲区的原因,这可能并不意味着数据会写入磁盘.这可能只是意味着数据从运行时维护的缓冲区复制到操作系统维护的缓冲区中.

However, due to the operating system buffers, this might not mean that the data is written to disk. It may just mean that the data is copied from the buffers maintained by your runtime into the buffers maintained by the operating system.

如果你写了一些东西,它最终进入了缓冲区(仅),并且你的机器电源被切断,当机器关闭时,该数据不在磁盘上.

If you write something, and it ends up in the buffer (only), and the power is cut to your machine, that data is not on disk when the machine turns off.

因此,为了帮助解决这个问题,您可以在各自的对象上使用 flushfsync 方法.

So, in order to help with that you have the flush and fsync methods, on their respective objects.

第一个,flush,将简单地将任何留在程序缓冲区中的数据写出到实际文件中.通常这意味着数据将从程序缓冲区复制到操作系统缓冲区.

The first, flush, will simply write out any data that lingers in a program buffer to the actual file. Typically this means that the data will be copied from the program buffer to the operating system buffer.

具体来说,这意味着如果另一个进程打开同一个文件进行读取,它将能够访问您刚刚刷新到文件中的数据.但是,这并不一定意味着它已永久"存储在磁盘上.

Specifically what this means is that if another process has that same file open for reading, it will be able to access the data you just flushed to the file. However, it does not necessarily mean it has been "permanently" stored on disk.

要做到这一点,您需要调用 os.fsync 方法,以确保所有操作系统缓冲区与它们用于的存储设备同步,换句话说,该方法将从操作系统缓冲到磁盘.

To do that, you need to call the os.fsync method which ensures all operating system buffers are synchronized with the storage devices they're for, in other words, that method will copy data from the operating system buffers to the disk.

通常情况下,您不需要使用任何一种方法,但如果您处于对磁盘上实际结束的内容的偏执是一件好事的情况下,您应该按照说明进行这两个调用.

Typically you don't need to bother with either method, but if you're in a scenario where paranoia about what actually ends up on disk is a good thing, you should make both calls as instructed.

2018 年的附录.

请注意,具有缓存机制的磁盘现在比 2013 年更常见,因此现在涉及更多级别的缓存和缓冲区.我假设这些缓冲区也会被同步/刷新调用处理,但我真的不知道.

Note that disks with cache mechanisms is now much more common than back in 2013, so now there are even more levels of caching and buffers involved. I assume these buffers will be handled by the sync/flush calls as well, but I don't really know.

这篇关于file.flush() 到底在做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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