码迷,mamicode.com
首页 > 编程语言 > 详细

单线程和多线程下载文件

时间:2020-06-03 16:01:34      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:family   time()   tar   hashlib   read   from   upload   pre   ros   

单线程下载文件:

import requests
import time
from hashlib import md5


def down_load_pic(url):
req = requests.get(url)
m = md5(url.encode())
with open(m.hexdigest() + ‘.png‘, ‘wb‘) as fw: # md5后的名称作为图片的前缀
fw.write(req.content)


url_list = [
‘http://www.nnzhp.cn/wp-content/uploads/2019/10/f410afea8b23fa401505a1449a41a133.png‘,
‘http://www.nnzhp.cn/wp-content/uploads/2019/11/481b5135e75c764b32b224c5650a8df5.png‘,
‘http://www.nnzhp.cn/wp-content/uploads/2019/11/b23755cdea210cfec903333c5cce6895.png‘,
‘http://www.nnzhp.cn/wp-content/uploads/2019/11/542824dde1dbd29ec61ad5ea867ef245.png‘,
]

start_time = time.time()
for url in url_list:
down_load_pic(url)
end_time = time.time()
print(end_time - start_time)

多线程下载文件:

import requests
import time
import threading
from hashlib import md5


def down_load_pic(url):
req = requests.get(url)
m = md5(url.encode())
with open(m.hexdigest() + ‘.png‘, ‘wb‘) as fw:
fw.write(req.content)


url_list = [
‘http://www.nnzhp.cn/wp-content/uploads/2019/10/f410afea8b23fa401505a1449a41a133.png‘,
‘http://www.nnzhp.cn/wp-content/uploads/2019/11/481b5135e75c764b32b224c5650a8df5.png‘,
‘http://www.nnzhp.cn/wp-content/uploads/2019/11/b23755cdea210cfec903333c5cce6895.png‘,
‘http://www.nnzhp.cn/wp-content/uploads/2019/11/542824dde1dbd29ec61ad5ea867ef245.png‘,
]


start_time = time.time()
for url in url_list:
t = threading.Thread(target=down_load_pic, args=(url,))
t.start()
while threading.activeCount() != 1:
pass
end_time = time.time()
print(end_time - start_time)

单线程和多线程下载文件

标签:family   time()   tar   hashlib   read   from   upload   pre   ros   

原文地址:https://www.cnblogs.com/laosun0204/p/13037634.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!