In many StackOverflow questions and answers like 1, 2, or ..., and many other places all around the web, people are using the following pattern to retrieve results using iter_content
:
for chunk in r.iter_content(chunk_size=512 * 1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
But is the if chunk:
condition really necessary, especially when the chunk size is a positive integer like here?
Doesn't the underlying code already handle this case? To me, it seems that it does, see 3, 4...
Is it still a good practice to check for empty chunks?
P.S. I know that the requests docs suggest to do this for iter_lines
:
for line in r.iter_lines():
# filter out keep-alive new lines
if line:
This question is only about iter_content
.
Update
It's also interesting to look at this the other way:
- BytesIO.write in Python 3 won't try to write an empty buffer and is essentially a no-op.
- In Python 2 it calls fwrite but fwrite in glibc has a check for empty buffers (don't know about other implementations though)
Thus people should probably not worry about these things, even when writing to a file using iter_lines
.