Built-in open() with buffering > 1
marco_u at nsgmail.com
Thu Aug 30 17:39:17 CEST 2012
On 08/26/2012 10:25 AM, Hans Mulder wrote:
> The algorithm is explained at
> In other words: open() tries to find a suitable size by
> calling os.stat(your_file).st_blksize and if that fails,
> it uses io.DEFAULT_BUFFER_SIZE, which is 8192 on my box.
Yes, when the parameter `buffering` is a negative integer
that is right
> Whether you call open with buffering=2 or any larger
> number, does not matter: the buffer size will be the
> outcome of this algorithm.
Mmm, I think it is not right, because in this case
the buffer size is not computed but it is
the value you assign to the buffering parameter.
>>> f = open('myfile', 'w', buffering=2)
>>> f._CHUNK_SIZE = 1
Now two bytes are in the buffer and the buffer is full.
If you write another byte, it will not be written in the
buffer, because the bytes in the queue will be transferred
into the buffer only when they are more than f._CHUNK_SIZE:
Now, if you write another byte 'd', the chunk 'cd' will
be transferred to the buffer, but because it is full,
its content 'ab' will be transferred to the disk, and
after 'cd' written to the buffer, that still full:
So, the buffer is really of size 2
More information about the Python-list