python线程池
Posted 听哥哥的话
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python线程池相关的知识,希望对你有一定的参考价值。
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)
以上是关于python线程池的主要内容,如果未能解决你的问题,请参考以下文章