https://blog.csdn.net/qq_33961117/article/details/82587873
#!/usr/bin/python
# -*- coding: utf-8 -*-
from concurrent.futures import ThreadPoolExecutor
from threading import current_thread
import time
import requests
def get(url):
print('%s GET %s' % (current_thread().name, url))
time.sleep(3)
response = requests.get(url)
if response.status_code == 200:
res = response.text
else:
res = '下载失败'
return res
def parse(future):
time.sleep(1)
res = future.result()
print('%s 解析结果为%s' % (current_thread().name, len(res)))
if __name__ == '__main__':
urls = [
'https://www.baidu.com',
'https://www.sina.com.cn',
'https://www.tmall.com',
'https://www.jd.com',
'https://www.python.org',
'https://www.openstack.org',
'https://www.baidu.com',
'https://www.baidu.com',
'https://www.baidu.com',
]
# 线程池内线程数
p = ThreadPoolExecutor()# ThreadPoolExecutor(4) 最多启用4个线程 不给则使用计算机的最大线程数
start = time.time()
for url in urls:
future = p.submit(get, url)
future.add_done_callback(parse)
p.shutdown(wait=True)
print('主', current_thread().name)
print('完成时间', time.time() - start)