Built-in open() with buffering > 1

M

Marco

Please, can anyone explain me the meaning of the
"buffering > 1" in the built-in open()?
The doc says: "...and an integer > 1 to indicate the size
of a fixed-size chunk buffer."
So I thought this size was the number of bytes or chars, but
it is not:
'abcdefghi\n'

Regards,
Marco
 
M

Marco

Please, can anyone explain me the meaning of the
"buffering > 1" in the built-in open()?
The doc says: "...and an integer > 1 to indicate the size
of a fixed-size chunk buffer."

Sorry, I get it:
.... n = f.write(str(i))
.... print(i, open('myfile').read(), sep=':')
....
0:
1:
2:
3:
4:
5:012345
 
R

Ramchandra Apte

`f._CHUNK_SIZE = 5` is modifying Python's internal variables - don't do that
google buffering to find out what it is
buffering is how much Python will keep in memory
f.read(1) will actually read `buffering` bytes of memory so that when you read later, the reading can be done from memory
 
H

Hans Mulder

Please, can anyone explain me the meaning of the
"buffering > 1" in the built-in open()?
The doc says: "...and an integer > 1 to indicate the size
of a fixed-size chunk buffer."
So I thought this size was the number of bytes or chars, but
it is not

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

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.

Whether you call open with buffering=2 or any larger
number, does not matter: the buffer size will be the
outcome of this algorithm.


Hope this helps,

-- HansM
 
M

Marco


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:

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:
'ab'

So, the buffer is really of size 2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top