问题
I am trying to understand the the buffering argument of the io.open() method in Python 2.7.
I execute in the Python interpreter:
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.
If buffer_size = 4000, I see that 9822 lines got written. However, buffer_size = 8192, I see that 8414 lines got written.
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). I don't understand where these numbers (9822 and 8414) come from.
回答1:
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). A negative buffering means to use the system default, which is usually line buffered for tty devices and fully buffered for other files. If omitted, the system default is used. [2]
I.e: 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.
来源:https://stackoverflow.com/questions/31869247/understanding-the-buffering-argument-of-the-io-open-method-in-python-2-7