python线程池的四种好处总结
作者:小妮浅浅 时间:2023-01-27 11:09:55
1、使用好处
提高性能:由于减去了大量新建终止线程的费用,重用了线程资源;
适用场景:适用于处理大量突发请求或需要大量线程完成任务,但实际任务处理时间短。
防御功能:可以有效避免系统因线程过多而导致系统负载过大而相应变慢的问题。
代码优势:使用线程池的语法比创建自己的线程更简单。
2、实例
"""
@file : 004-线程池的使用.py
@author : xiaolu
@email : luxiaonlp@163.com
@time : 2021-02-01
"""
import concurrent.futures
import requests
from bs4 import BeautifulSoup
def craw(url):
# 爬取网页内容
r = requests.get(url)
return r.text
def parse(html):
# 解析其中的内容
soup = BeautifulSoup(html, "html.parser")
links = soup.find_all("a", class_="post-item-title")
return [(link["href"], link.get_text()) for link in links] # 那链接和标题拿出来
if __name__ == '__main__':
# 待爬取的网页链接
urls = [
"https://www.cnblogs.com/sitehome/p/{}".format(page) for page in range(1, 50 + 1)
]
# craw
with concurrent.futures.ThreadPoolExecutor() as pool:
htmls = pool.map(craw, urls)
htmls = list(zip(urls, htmls))
for url, html in htmls:
print(url, len(html))
print("craw over")
# parse
with concurrent.futures.ThreadPoolExecutor() as pool:
futures = {}
for url, html in htmls:
future = pool.submit(parse, html)
futures[future] = url
# for future, url in futures.items():
# print(url, future.result())
for future in concurrent.futures.as_completed(futures):
url = futures[future]
print(url, future.result())
知识点补充:
线程池的使用
线程池的基类是 concurrent.futures 模块中的 Executor,Executor 提供了两个子类,即 ThreadPoolExecutor 和ProcessPoolExecutor,其中 ThreadPoolExecutor 用于创建线程池,而 ProcessPoolExecutor 用于创建进程池。
如果使用线程池/进程池来管理并发编程,那么只要将相应的 task 函数提交给线程池/进程池,剩下的事情就由线程池/进程池来搞定。
Exectuor 提供了如下常用方法:
submit(fn, *args, **kwargs):将 fn 函数提交给线程池。*args 代表传给 fn 函数的参数,*kwargs 代表以关键字参数的形式为 fn 函数传入参数。
map(func, *iterables, timeout=None, chunksize=1):该函数类似于全局函数 map(func, *iterables),只是该函数将会启动多个线程,以异步方式立即对 iterables 执行 map 处理。
shutdown(wait=True):关闭线程池。
程序将 task 函数提交(submit)给线程池后,submit 方法会返回一个 Future 对象,Future 类主要用于获取线程任务函数的返回值。由于线程任务会在新线程中以异步方式执行,因此,线程执行的函数相当于一个“将来完成”的任务,所以 Python 使用 Future 来代表。
来源:https://www.py.cn/jishu/jichu/30058.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL修改root账号密码的方法
![](https://img.aspxhome.com/file/2023/4/103384_0s.png)
Python通过两个dataframe用for循环求笛卡尔积
![](https://img.aspxhome.com/file/2023/5/79555_0s.png)
Python使用graphviz画流程图过程解析
![](https://img.aspxhome.com/file/2023/0/72040_0s.jpg)
python3实现163邮箱SMTP发送邮件
![](https://img.aspxhome.com/file/2023/8/63538_0s.jpg)
mysql中的mvcc 原理详解
![](https://img.aspxhome.com/file/2023/0/74860_0s.jpg)
在MySQL中为何不建议使用utf8
![](https://img.aspxhome.com/file/2023/5/126305_0s.jpg)
python爬虫之验证码篇3-滑动验证码识别技术
![](https://img.aspxhome.com/file/2023/2/64702_0s.gif)
如何区分SQL数据库中的主键与外键
BootStrap 页签切换失效的解决方法
![](https://img.aspxhome.com/file/2023/8/136068_0s.jpg)
微信小程序开发注意指南和优化实践(小结)
![](https://img.aspxhome.com/file/2023/7/136217_0s.jpg)
Python使用中文正则表达式匹配指定中文字符串的方法示例
![](https://img.aspxhome.com/file/2023/0/86430_0s.png)
Mysql双主搭建的方法步骤
![](https://img.aspxhome.com/file/2023/0/128420_0s.png)
Python实现滑雪小游戏
![](https://img.aspxhome.com/file/2023/7/66397_0s.gif)
MSSQL数据库占用内存过大造成服务器死机问题的解决方法
![](https://img.aspxhome.com/file/2023/5/99965_0s.jpg)
Java+MySql图片数据保存与读取的具体实例
Git 教程之创建仓库详解
利用打码兔和超人打码自封装的打码类分享
深入解析MySQL索引数据结构
![](https://img.aspxhome.com/file/2023/9/116749_0s.png)