简体   繁体   English

了解Python 2.7中的io.open()方法的缓冲参数

[英]Understanding the buffering argument of the io.open() method in Python 2.7

I am trying to understand the the buffering argument of the io.open() method in Python 2.7. 我试图了解python 2.7中io.open()方法的缓冲参数。

I execute in the Python interpreter: 我在Python解释器中执行:

import utils
buffer_size = 4000
file = open('test.txt','w', buffer_size)
file.write('\n'.join(map(str, range(10000))))  

then I look at the test.txt file to see how many lines got written, even though I haven't called file.close() yet, and didn't do any manual file.flush() myself. 然后我看一下test.txt文件,看写了多少行,即使我还没有调用file.close() ,也没有自己做任何手动的file.flush()

If buffer_size = 4000 , I see that 9822 lines got written. 如果buffer_size = 4000 ,我看到写入了9822行。 However, buffer_size = 8192 , I see that 8414 lines got written. 但是, buffer_size = 8192 ,我看到写入了8414行。

I get this behavior in both Windows 7 SP1 x64 Ultimate (Python 2.7.10 x64) and Kubuntu 14.10 Plasma 4 (Python 2.7.10 x64). 我在Windows 7 SP1 x64 Ultimate(Python 2.7.10 x64)和Kubuntu 14.10 Plasma 4(Python 2.7.10 x64)中都得到了这种行为。 I don't understand where these numbers (9822 and 8414) come from. 我不知道这些数字(9822和8414)从何而来。

Quote from the documentation (emphasis is mine): 文档引用(重点是我的):

The optional buffering argument specifies the file's desired buffer size: 0 means unbuffered, 1 means line buffered, any other positive value means use a buffer of (approximately) that size (in bytes) . 可选的buffering参数指定文件所需的缓冲区大小:0表示未缓冲,1表示行缓冲, 任何其他正值 表示使用(大约)该大小(以字节为单位)的缓冲区 A negative buffering means to use the system default, which is usually line buffered for tty devices and fully buffered for other files. 负缓冲意味着使用系统默认值,通常对tty设备使用行缓冲,而对于其他文件则使用完全缓冲。 If omitted, the system default is used. 如果省略,则使用系统默认值。 [2] [2]

Ie: the buffer size is not guaranteed to be what you pass as parameter. 即:不能保证缓冲区大小是您作为参数传递的大小。 It's impossible to predict how much of the buffer is in use and how much has been written to disk as your write overflow the buffer in both case and the buffer size is machine dependent. 由于两种情况下缓冲区的写操作都会溢出,并且缓冲区的大小取决于计算机,因此无法预测正在使用多少缓冲区以及已将多少缓冲区写入磁盘。

As you didn't call an explicit flush, part of the buffer has been flushed and another part is still waiting for it to fill before flushing to disk. 由于您没有调用显式刷新,因此缓冲区的一部分已被刷新,另一部分仍在等待其填充,然后再刷新到磁盘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM