我有这个简单的 python 脚本,它使用 curl 下载文件。它还计算带宽。这是整个脚本:
import pycurl
import time
next_time = 0
# Callback function invoked when download/upload has progress
def progress(download_t, download_d, upload_t, upload_d):
global next_time
if time.time() >= next_time:
print("Total to download", download_t)
print("Total downloaded", download_d)
print("Total to upload", upload_t)
print("Total uploaded", upload_d)
download_speed = (download_d / 1000000 * 8) / 10
print(" download_speed", download_speed)
next_time = time.time() + 10
c = pycurl.Curl()
c.setopt(c.URL, "https://mysample_domain.com/speed-test/test/1G.dat")
c.setopt(c.NOPROGRESS, False)
c.setopt(c.XFERINFOFUNCTION, progress)
start_time = time.time()
next_time = time.time() + 10
c.perform()
print('after download, duration = ', time.time() - start_time)
问题是,当我使用回调进度时,文件下载时间为 459 秒。当我删除这两行时,文件在 210 秒内下载完毕。为什么我在方法进行中的计算会冻结下载速度?
c.setopt(c.NOPROGRESS, False)
c.setopt(c.XFERINFOFUNCTION, progress)