【问题标题】:Show the download speed in Google drive Python在 google drive python 中显示下载速度
【发布时间】:2021-11-16 16:03:03
【问题描述】:

如何在google drive api python中显示下载速度

我想修改一下以显示下载速度

def download_file(id):
    fileStats=file_info(id)
    # showing the file stats
    print("----------------------------")
    print("FileName: ",fileStats['name'])
    print("FileSize: ",convert_size(int(fileStats['size'])))
    print("----------------------------")

    request = service.files().get_media(fileId=id)
    fh = io.BytesIO()
    downloader = MediaIoBaseDownload(fh, request,chunksize=1048576)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        # just a function that clear the screen and displays the text passed as argument
        ui_update('{1:<10}\t{2:<10}\t{0}'.format(fileStats['name'],color(convert_size(int(fileStats['size'])),Colors.orange),color(f'{round(status.progress()*100,2)}%',Colors.green)))

        fh.seek(0)
        with open(os.path.join(location, fileStats['name']), 'wb') as f:
            f.write(fh.read())
            f.close()
    else:
        print("File Download Cancelled!!!")

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: python google-drive-api downloadfile download-speed


【解决方案1】:

您可以自己计算速度。您知道块大小,因此您知道正在下载多少;下载速度的时间。

类似:

from time import monotonic
...
CHUNKSIZE=1048576
while not done:
    start = monotonic()
    status, done = downloader.next_chunk()
    speed = CHUNKSIZE / (monotonic() - start)

或者,根据您的 gui,使用适当的进度条库(它会在内部执行类似的操作)。

请注意,非常长的行很难阅读。这个:

ui_update('{1:<10}\t{2:<10}\t{0}'.format(fileStats['name'],color(convert_size(int(fileStats['size'])),Colors.orange),color(f'{round(status.progress()*100,2)}%',Colors.green)))

应该写成这样:

name = fileStats["name"]
size = color(convert_size(int(fileStats["size"])), Color.orange)
progress = round(status.progress() * 100, 2)
progress = color(f"{progress} %", colors.green)
ui_update('{1:<10}\t{2:<10}\t{0}'.format(name, size, progress))

现在我们可以阅读它,我们可以看到只有进度值会改变。所以将namesize 移出你的循环并且只调用它们一次。

参考文献

https://docs.python.org/3/library/time.html#time.monotonic

【讨论】:

  • monotonic() - time 是什么意思?在这种情况下,time 是什么?
  • 查看链接文档(刚刚添加)。谢谢,你打错了,对不起。我还发现我计算了 1/download speed....
猜你喜欢
  • 2019-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-07
  • 2019-01-12
相关资源
最近更新 更多