Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add progress to streaming download #1268

Merged
merged 11 commits into from
Sep 10, 2020

Conversation

cdeler
Copy link
Member

@cdeler cdeler commented Sep 8, 2020

Having found #1208, I tried to track last downloaded raw chunk of the response.

The question is:

  1. should we also track decoded response chunks? I don't see any reason since decoded stream length might be vary from the raw stream length, and it has no bearing with downloading process (but may be I missed something)

Copy link
Member

@florimondmanca florimondmanca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still need to ponder on this a bit, but very neat implementation (code, docs and tests)!

docs/advanced.md Outdated Show resolved Hide resolved
docs/advanced.md Outdated Show resolved Hide resolved
@florimondmanca
Copy link
Member

should we also track decoded response chunks?

Don't think so, since that's directly available as len(chunk) already.

cdeler and others added 2 commits September 9, 2020 12:31
@tomchristie
Copy link
Member

Great, thanks!

I think this has helped me. understand why I'm a bit wary of response.last_raw_chunk_size as the interface here, rather than response.num_bytes_downloaded (Or something similarly named.)

It seems to me that it's more likely to lead to brittle code. Also it's not super clear what the behaviour is expected to be in different cases. Eg...

In contrast if we used response.num_bytes_downloaded then there's just less to get wrong.
Also examples like this work, where .last_raw_chunk_size wouldn't...

response = httpx.get(...)
response.num_bytes_downloaded  # Determine the final download size.

It is true that num_bytes_downloaded happens to be marginally more work in the tqdm example, but it's not much more to deal with.

num_bytes_downloaded = 0
with httpx.stream("...") as response:
    total = ...
    with tqdm(total=...) as progress:
        for chunk in response.iter_bytes():
            download_file.write(chunk)
            progress.update(response.num_bytes_downloaded - num_bytes_downloaded)
            num_bytes_downloaded = response.num_bytes_downloaded

What do we think?

docs/advanced.md Outdated Show resolved Hide resolved
docs/advanced.md Outdated Show resolved Hide resolved
docs/advanced.md Outdated Show resolved Hide resolved
Copy link
Member

@tomchristie tomchristie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lovely bit of functionality to work towards yup!
Really liking the docs example here.
Possible that we ought to be going for a total downloaded bytes API rather than a chunk size API?

Changed last_raw_chunk_size to num_bytes_downloaded ;
Edited the example according to documentaion
@cdeler
Copy link
Member Author

cdeler commented Sep 9, 2020

@tomchristie

I changed last_raw_chunk_size to num_bytes_downloaded.

For now we can state that there is a correlation between the amount of data (lets say N) drained from iter_raw(chunk_size=123) and num_bytes_downloaded. I.e. until the end of the stream we can assert that N <= response.num_bytes_downloaded. And well, we cannot say the same about iter_bytes(chunk_size=123) due to compression.

docs/advanced.md Outdated Show resolved Hide resolved
Copy link
Member

@florimondmanca florimondmanca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good with num_bytes_downloaded too, yup 👍

docs/advanced.md Outdated Show resolved Hide resolved
docs/advanced.md Outdated Show resolved Hide resolved
cdeler and others added 3 commits September 9, 2020 22:42
Co-authored-by: Florimond Manca <florimond.manca@gmail.com>
Co-authored-by: Florimond Manca <florimond.manca@gmail.com>
docs/advanced.md Outdated Show resolved Hide resolved
@tomchristie
Copy link
Member

Have made a couple of minor docs tweaks.
Looking great, thank you!

@cdeler cdeler merged commit ed16eb3 into encode:master Sep 10, 2020
@cdeler cdeler deleted the add-progress-to-streaming-download branch September 10, 2020 09:16
@tomchristie tomchristie mentioned this pull request Sep 21, 2020
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants