python之线程池
ThreadPoolExecutor(线程池)
线程池一些知识点:
- python中ThreadPoolExecutor(线程池)是concurrent.futures模块下的,主线程中可以获取某一个线程执行的状态或者某一个任务执行的状态及返回值。
- 通过submit返回的是一个future对象,它是一个未来可期的对象,通过它可以获悉线程的状态
- 通过submit提交函数到进程池中,submit(函数名,参数)
- 通过wait()判断线程执行的状态:wait(fs, timeout=None, return_when=ALL_COMPLETED),wait接受3个参数,fs表示执行的task序列;timeout表示等待的最长时间,超过这个时间即使线程未执行完成也将返回;return_when表示wait返回结果的条件,默认为ALL_COMPLETED全部执行完成再返回
- http的是IO请求,所以用线程
- 编写方法一:直接返回处理
from concurrent.futures import ThreadPoolExecutor import requests pool = ThreadPoolExecutor(10) def task(url): response = requests.get(url) print(url,response) url_list = ( "https://www.bing.com", "https://www.shihu.com", "https://www.sina.com", "https://www.baidu.com", "https://www.cnblogs.com", "https://music.163.com/#" ) for url in url_list: pool.submit(task,url) pool.shutdown(wait=True)
result
https://www.bing.com <Response [200]> https://www.baidu.com <Response [200]> https://www.cnblogs.com <Response [200]> https://music.163.com/# <Response [200]> https://www.sina.com <Response [200]>
- 编写方法二:通过回调函数进行处理
from concurrent.futures import ThreadPoolExecutor import requests pool = ThreadPoolExecutor(10) def task(url): response = requests.get(url) #下载页面 return response def done(future,*args,**kwargs): response = future.result() #取得future对象进行操作 print(response.status_code,response.content) url_list = ( "https://www.bing.com", "https://www.jd.com", "https://www.sina.com", "https://www.baidu.com", "https://www.cnblogs.com", "https://music.163.com/#" ) for url in url_list: v = pool.submit(task,url) #获得一个返回值,其实就是访问url的响应 v.add_done_callback(done)
result
响应信息balabalabala ps:太多就不发了