多线程下载文件

import request,threading,hashlib,time
def down_load_file(url):
    r=request.get(url)
    m=hashlib.md5(url.encode()) 
    print('开始下载%s'%m.hexdigest())
    with open ('%s.jpg'%m.hexdigest(),wb) as fw
        fw.write(r.content)

url_list = ['http://www.nnzhp.cn/wp-content/uploads/2019/02/jiami.jpeg',
            'http://www.nnzhp.cn/wp-content/uploads/2019/03/js.png',
            'http://www.nnzhp.cn/wp-content/uploads/2018/08/ab389f04cb5b57344ef9655428bccaec.png'
            ]

start_time=time.time()
#单线程
#list(map(down_load_file,url_list))   # map():循环取值并调用
#多线程
for i in range(2):
    t=threading.Thread(target=down_load_file,args=(url,)) #如果args传参只传一个参数,参数后边要加逗号(,)否则无法识别是一个list
    t.start()
while threading.activeCount() !=1:
    pass
print(time.time()-start_time)

 

posted @ 2019-07-25 14:39  4jd121de2gf4e2sa5d  阅读(127)  评论(0编辑  收藏  举报