Why to use iter_content and chunk_size in python requests

Andriken sonwane picture Andriken sonwane · Sep 13, 2017 · Viewed 22.3k times · Source

Why should I use iter_content and specially I'm really confused with the purpose using of chunk_size , as I have tried using it and in every way the file seems to be saved after downloading successfully.

g = requests.get(url, stream=True)

with open('c:/users/andriken/desktop/tiger.jpg', 'wb') as sav:
    for chunk in g.iter_content(chunk_size=1000000):
        print (chunk)
        sav.write(chunk)

Help me understand the use of iter_content and what will happen as you see I am using 1000000 bytes as chunk_size, what is the purpose exactly and results?

Answer

cowbert picture cowbert · Sep 13, 2017

This is to prevent loading the entire response into memory at once (it also allows you to implement some concurrency while you stream the response so that you can do work while waiting for request to finish).

The purpose of setting streaming request is usually for media. Like try to download a 500 MB .mp4 file using requests, you want to stream the response (and write the stream in chunks of chunk_size) instead of waiting for all 500mb to be loaded into python at once.

If you want to implement any UI feedback (such as download progress like "downloaded <chunk_size> bytes..."), you will need to stream and chunk. If your response contains a Content-Size header, you can calculate % completion on every chunk you save too.