下载图像需要很多时间

时间:2019-02-01 09:57:41

标签: python python-3.x python-requests

我使用以下方法下载图像:

                newlink = image.img['src']
                print('Downloading image', index)
                try:
                    response = requests.get(newlink, stream=True)
                    sleep(1)
                    with open(image_path, 'wb') as file:
                        sleep(1)
                        shutil.copyfileobj(response.raw, file)
                except Exception as e:

                    print(e)
                    print('Could not download image number ', index)

一切正常,但是我已经注意到,每天运行脚本时,几天(5-7天)后,下载每个图像会花费很多时间。发生这种情况时,我关闭了pycharm并重新启动了PC。之后,它又开始正常工作。

我想知道是否有人知道为什么会这样。

谢谢

1 个答案:

答案 0 :(得分:0)

这可能是内存或网络堆栈问题。根据文档: http://docs.python-requests.org/en/master/user/advanced/

如果在发出请求时将流设置为True,则除非消耗掉所有数据或调用Response.close,否则请求无法将连接释放回池。这可能导致连接效率低下。如果在使用stream = True时发现自己部分读取了请求正文(或根本不读取它们),则应在with语句中发出请求以确保始终关闭该请求:

with requests.get('https://httpbin.org/get', stream=True) as r:
    # Do things with the response here.

尝试一下:

newlink = image.img['src']
print('Downloading image', index)
try:
    with requests.get(newlink, stream=True) as response:
        sleep(1)
        with open(image_path, 'wb') as file:
            sleep(1)
            shutil.copyfileobj(response.raw, file)
except Exception as e:

    print(e)
    print('Could not download image number ', index)