多线程下载图片

import requests,threading,time
from hashlib import md5

urls = [
    'http://www.178linux.com/wp-content/uploads/2018/02/5.jpg',
    'http://img1.imgtn.bdimg.com/it/u=1139158180,2224775217&fm=11&gp=0.jpg',
    'http://www.linuxidc.com/upload/2019_04/19041915053582.png',
    'http://www.veryxue.com/file/upload/201905/09/201804031578.jpg',
    'http://5b0988e595225.cdn.sohucs.com/images/20171209/8e81dcb041a9425c823daf6b6053e03b.jpg'
]
result=[]
def down_load_picture(url):
    r=requests.get(url)
    file_name=md5(r.content).hexdigest()
    with open(file_name+".jpg",'wb') as fw:
        fw.write(r.content)
        result.append(file_name)
        print("[%s]下载完成"%file_name)

start_time = time.time()
for url in urls:
    #down_load_picture(url)
    t=threading.Thread(target=down_load_picture,args=(url,))
    t.start()

while threading.activeCount()!=1:  # 判断当前系统是否只有一个主线程
    pass
end_time = time.time()
print(result)
run_time= end_time - start_time
print("运行的时间:%s"%run_time)

 

posted on 2019-06-01 13:39  dongxl  阅读(93)  评论(0编辑  收藏  举报

导航