【问题标题】:Threadpool for image downloading用于图像下载的线程池
【发布时间】:2022-08-20 18:32:03
【问题描述】:

如何使用线程池更快地下载 1000 多张图像?因为用我当前的脚本下载这 1000 多张图片需要很长时间

当前脚本

import request

image_url = [
      “http://image_eg_001”,
      “http://image_eg_002”,
      “http://image_eg_003”,

]

for img in image_url:
   file_name = img.split(‘/‘)[-1]
   print(“Downloading File:%s”%file_name)
   r = request.get(img, stream=True)
   with open(file_name, ‘wb’) as f:
     for chunk in r:
     f.write(chunk)

标签: python multithreading image pool bulk


【解决方案1】:

您可以使用AsyncIOAIOHTTP 包来执行并发网络请求。一个潜在的解决方案是:

import asyncio
import aiohttp

async def download_image(image_url: str, save_path: str, session: aiohttp.ClientSession):
  async with session.get(image_url) as response:
    content = await response.read()
    with open(save_path, "wb") as f:
      f.write(content)

async def main():
  image_urls = [...]
  save_paths = [...]

  async with aiohttp.ClientSession() as session:
    await asyncio.gather(*[download_image(im, p, session) for im, p in zip(image_urls, save_paths)])    

if __name__ == "__main__":
  asyncio.run(main())

download_image() 函数负责下载和保存一张图片。

main() 函数使用 asyncio.gather() 执行并发请求。

【讨论】:

    【解决方案2】:

    您可以使用concurrent.futures.ThreadPoolExecutor 类。我选择了 100 作为工作线程数,但您可以根据您的系统更改它,它可以或多或少根据您的情况而定。如果下载需要很长时间,更多的工作线程会严重影响您的响应能力和系统资源。

    这里是下载图片的线程池解决方案

    import requests
    from concurrent.futures import ThreadPoolExecutor
    
    image_url = [
          'http://image_eg_001',
          'http://image_eg_002',
          'http://image_eg_003',
    
    ]
    
    def download(url):
        r = requests.get(url, allow_redirects=False)
    
        with open(url.split("/")[-1], "wb") as binary:
            binary.write(r.content)
    
    
    with ThreadPoolExecutor(max_workers=100) as executor:
        executor.map(download,image_url)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      相关资源
      最近更新 更多