You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python使用threading/multiprocessing实现图片并发下载问题求助

问题诊断
  • 核心错误:创建Process/Thread对象时,target参数直接填写了download_chunk(0),会在主线程/主进程中立即同步执行该函数,并未把函数本身传递给子任务。所有下载任务会串行执行完成后才会创建子进程/子线程,而子任务最终拿到的target是函数返回的None,无实际逻辑可执行,因此完全没有并发效果。
  • 次要问题:divide_chunks函数内部误用了全局变量classes,而非传入的参数l,如果后续传入其他列表做分片会出现逻辑错误。
可行实现方案

下载属于IO密集型任务,使用多线程即可获得足够的并发收益,且资源开销远低于多进程,优先推荐多线程方案。

修正后的基础多线程实现

import threading

# 修正分片函数逻辑
def divide_chunks(l, n):
    for i in range(0, len(l), n):
        yield l[i:i + n]

classes = list(divide_chunks(classes, 25))

def download_chunk(n):
    for label in classes[n]:
        try:
            downloader.download(label, limit=1000, output_dir='dataset', adult_filter_off=True, force_replace=False,verbose=True)
        except:
            pass

# 正确创建并启动线程:target传递函数名,args传递参数元组
threads = []
for chunk_idx in range(4):
    t = threading.Thread(target=download_chunk, args=(chunk_idx,))
    threads.append(t)
    t.start()

# 等待所有线程执行完成
for t in threads:
    t.join()

更简洁的线程池实现

使用concurrent.futures封装的线程池,代码更简洁易维护:

from concurrent.futures import ThreadPoolExecutor

# 分片函数和download_chunk保持上述修正后的版本即可
with ThreadPoolExecutor(max_workers=4) as executor:
    executor.map(download_chunk, [0,1,2,3])

修正后的多进程实现(仅推荐CPU密集型场景使用)

注意Windows环境下多进程代码必须放在if __name__ == '__main__'入口判断下执行:

from multiprocessing import Process

# 分片函数和download_chunk保持上述修正后的版本即可
if __name__ == '__main__':
    processes = []
    for chunk_idx in range(4):
        p = Process(target=download_chunk, args=(chunk_idx,))
        processes.append(p)
        p.start()
    
    for p in processes:
        p.join()

内容的提问来源于stack exchange,提问作者mchd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 21:15:02