Built-in open() with buffering > 1

Marco marco_u at nsgmail.com
Thu Aug 30 11:39:17 EDT 2012


On 08/26/2012 10:25 AM, Hans Mulder wrote:

> The algorithm is explained at
> http://docs.python.org/library/io.html#io.DEFAULT_BUFFER_SIZE

Thanks ;)

> 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.
In fact:

 >>> f = open('myfile', 'w', buffering=2)
 >>> f._CHUNK_SIZE = 1
 >>> f.write('ab')
2
 >>> open('myfile').read()

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:

 >>> f.write('c')
1
 >>> open('myfile').read()

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:

 >>> f.write('d')
1
 >>> open('myfile').read()
'ab'

So, the buffer is really of size 2




More information about the Python-list mailing list