multiprocessing和tqdm配合使用(多进程下载文件进度条显示)
代码
import multiprocessing as mp
import platform
from tqdm import tqdm
import wget
ls = ['url1', 'url2', 'url3'] # 这里填入实际要下载的url
pbar = tqdm(total=len(ls))
pbar.set_description('Sleep')
update = lambda *args: pbar.update()
# mac上好像要加上这个
mp.set_start_method('forkserver', force=True)
# 有多少个核心就开多少个进程
n_proc = mp.cpu_count()
pool = mp.Pool(n_proc)
for d in ls:
pool.apply_async(wget.download, (d, './download', ), callback=update)
pool.close()
pool.join()