我有这个简单的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)发布于 2019-01-08 01:42:12
我已经分析了您的代码,并得出结论,进度函数不会导致任何显著的速度减慢。事实上,您的代码只在进度函数中花费了总运行时间的0.2% (在20秒的下载过程中有大约40k的调用)。
我还多次运行下载,并注意到有时没有进度的下载,有时有进度的下载花费的时间比平时要长得多(大约是平时的2-3倍)。这可能是由于网络问题/瓶颈,或者pycurl的一些问题,但它肯定不是由您的进度函数引起的。
https://stackoverflow.com/questions/54078989
复制相似问题