Python 的 SpooledTemporaryFile 到底是什么?

What exactly is Python's SpooledTemporaryFile?

我正在查看 Python 中的 tempfile 选项,当我 运行 进入 SpooledTemporaryFile 时,现在的描述是:

This function operates exactly as TemporaryFile() does, except that data is spooled in memory until the file size exceeds max_size, or until the file’s fileno() method is called, at which point the contents are written to disk and operation proceeds as with TemporaryFile().

我想确切地理解这意味着什么,我环顾四周但没有找到答案,如果我没看错的话:

写入的数据'buffered'是在RAM中,直到达到某个阈值,然后保存在磁盘中?这种方法比标准方法有什么优势?它更快吗?因为最后无论如何都要存盘...

无论如何,如果有人能提供一些见解,我将不胜感激。

数据在写入前缓存在内存中。问题变成了, 会写什么吗?

不一定。 TemporaryFile,默认情况下,文件关闭后删除。如果假脱机文件的大小永远无法写入磁盘(并且用户永远不会尝试调用其 fileno 方法),则缓冲区将在关闭时简单地刷新,而不会实际占用磁盘 I/O地方。