我有这个简单的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)
答案 0 :(得分:0)
我已经分析了您的代码,并得出结论,进度功能不会引起任何明显的下降。实际上,您的代码仅在进度函数中花费了总运行时间的0.2%(在20秒的下载过程中调用了约40k)。
我也多次运行下载,并且注意到有时下载没有进度,有时下载进度比平时要长得多(大约是正常时间的2-3倍)。这可能是由于网络问题/瓶颈或pycurl的某些问题引起的,但这绝对不是由您的进度功能引起的。