1. 程式人生 > >python線程池

python線程池

parse == for lee tails aid net bsp text

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線程池