[英]Python async iterator (httpx) and tqdm.asyncio (download progress bar)
我將 httpx 用作 AsyncClient()(稱為 http)並希望顯示下載進度。
async with self.http.stream(method='GET', url=download_url) as res:
file_out = open(file_path, 'wb')
async for chunk in tqdm.asyncio.tqdm(iterable=res.aiter_bytes(),
desc=name, unit='iB',unit_scale=True, unit_divisor=1024, total=size):
file_out.write(chunk)
file_out.close()
下載工作正常,進度條確實顯示了一些進度,但與提供的比例無關。
結果:
test.mov: 0%| | 169/2.52M [00:07<32:42:46, 21.4iB/s]
顯示了正確的總大小,但顯然單位不同。
如果使用特定的塊大小,進度也無法正確顯示:
async with self.http.stream(method='GET', url=download_url) as res:
file_out = open(file_path, 'wb')
async for chunk in tqdm.asyncio.tqdm(iterable=res.aiter_bytes(chunksize),
desc=name, unit='iB',unit_scale=True, unit_divisor=1024, total=size):
file_out.write(chunk)
file_out.close()
然后進度條將遍歷塊(塊計數),但為字節設置的比例不起作用,例如對於 10 MB 文件:
test.mov: 0%| | 2.00/10.0M [00:35<51795:37:19, 17.8s/iB
最接近字節流的結果是省略了塊大小,但該單元已關閉。
關於如何顯示正確進度的任何想法?
謝謝!
通過將 1 作為單位(字節)傳遞來解決:
async with self.http.stream(method='GET', url=download_url) as res:
file_out = open(file_path, 'wb')
async for chunk in tqdm.asyncio.tqdm(iterable=res.aiter_bytes(1),
desc=name, unit='iB',unit_scale=True, unit_divisor=1024, total=size):
file_out.write(chunk)
file_out.close()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.