Skip to content

Instantly share code, notes, and snippets.

@balki
Last active November 3, 2022 20:08
Show Gist options
  • Save balki/193af5b8f508ec5eefbded13b0c73634 to your computer and use it in GitHub Desktop.
Save balki/193af5b8f508ec5eefbded13b0c73634 to your computer and use it in GitHub Desktop.
httpx tqdm example
"""
Run as below:
❯ pip install pex
❯ pex httpx tqdm -- ./example.py
"""
import asyncio, tqdm, httpx
async def get_chunks_with_progress(response: httpx.Response):
total = int(response.headers["Content-Length"])
num_bytes_downloaded = response.num_bytes_downloaded
with tqdm.tqdm(total=total, unit_scale=True, unit_divisor=1024, unit='B') as progress:
async for chunk in response.aiter_bytes():
yield chunk
progress.update(response.num_bytes_downloaded - num_bytes_downloaded)
num_bytes_downloaded = response.num_bytes_downloaded
async def a_download():
url = "https://archive.org/download/stackexchange/beer.stackexchange.com.7z"
async with httpx.AsyncClient() as client:
async with client.stream("GET", url) as response:
with open('/tmp/dump.7z', 'wb') as fp:
async for chunk in get_chunks_with_progress(response):
fp.write(chunk)
asyncio.run(a_download())
"""
Some relevant links:
pex: https://github.com/pantsbuild/pex
tqdm: https://github.com/tqdm/tqdm
httpx: https://www.python-httpx.org/
httpx doc for the example: https://www.python-httpx.org/advanced/#monitoring-download-progress
Why we can't just use `progress.update(len(chunk))`?
Answer: Compression
Explanation: https://github.com/encode/httpx/pull/1565#issuecomment-817098129
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment