当前位置: 首页 > news >正文

什么时候会用到 concurrent.futures?要不要背?

什么时候会用到 concurrent.futures?要不要背?

    • 一、到底什么时候会用到它?
    • 二、哪些 API 必须背下来?
      • 1. 创建线程池:`ThreadPoolExecutor`
      • 2. 提交单个任务:`submit()` + `Future`
      • 3. 等最快先完成:`as_completed()`
      • 4. 统一收集结果(顺序固定):`executor.map()`
      • 5. 任务完成后自动回调:`add_done_callback()`
      • 6. 取消任务:`cancel()`
      • 7. CPU 密集场景换进程池:`ProcessPoolExecutor`
    • 三、易踩的坑(背下来少加班)
    • 四、总结背诵清单


一、到底什么时候会用到它?

场景举例是否必须掌握
IO 密集型并发同时爬 100 个网页、并发下载、并发调用第三方 API✅ 推荐掌握
CPU 密集型并行图像批量压缩、科学计算(NumPy 之外的部分)✅ 推荐掌握
简化 Thread/Process 手写不想手动写 threading.Threadmultiprocessing.Process✅ 推荐掌握
异步回调需求任务完成后自动写日志、发通知✅ 推荐掌握
需要超时/取消任务爬虫里 3 秒还没下完就放弃✅ 推荐掌握

一句话:只要你写过“for 循环逐个处理,太慢!”就值得用 concurrent.futures 来提速/简化代码。


二、哪些 API 必须背下来?

下面把真正常用、面试常问、开发必会的 7 个核心点提炼出来,并给出最小可运行示例。
复制即可跑,建议收藏+背诵。


1. 创建线程池:ThreadPoolExecutor

from concurrent.futures import ThreadPoolExecutor
import requestsURLS = ["https://www.baidu.com","https://www.zhihu.com","https://www.bilibili.com",
]def fetch(url):return requests.get(url, timeout=5).status_code# ===== 背:最常用的 3 行 =====
with ThreadPoolExecutor(max_workers=3) as pool:for status in pool.map(fetch, URLS):print(status)

2. 提交单个任务:submit() + Future

from concurrent.futures import ThreadPoolExecutorpool = ThreadPoolExecutor()
future = pool.submit(pow, 2, 10)   # 2**10
print(future.result())             # 1024
pool.shutdown()                    # 别忘了,或用 with

3. 等最快先完成:as_completed()

from concurrent.futures import ThreadPoolExecutor, as_completed
import time, randomdef work(x):time.sleep(random.random())return x * xwith ThreadPoolExecutor() as pool:futures = [pool.submit(work, i) for i in range(5)]for f in as_completed(futures):         # 谁先完就处理谁print(f"done: {f.result()}")

4. 统一收集结果(顺序固定):executor.map()

with ThreadPoolExecutor() as pool:squares = list(pool.map(lambda x: x**2, range(5)))
print(squares)   # [0, 1, 4, 9, 16] 顺序与输入一致

5. 任务完成后自动回调:add_done_callback()

def callback(fut):print("Task finished, result =", fut.result())with ThreadPoolExecutor() as pool:f = pool.submit(lambda: 42)f.add_done_callback(callback)

6. 取消任务:cancel()

with ThreadPoolExecutor(max_workers=2) as pool:future = pool.submit(time.sleep, 10)print("cancel success?", future.cancel())  # True/False

7. CPU 密集场景换进程池:ProcessPoolExecutor

from concurrent.futures import ProcessPoolExecutor
import mathdef is_prime(n):return n > 1 and all(n % i for i in range(2, int(math.sqrt(n)) + 1))with ProcessPoolExecutor() as pool:primes = list(pool.map(is_prime, range(100)))
print(sum(primes))  # 25

三、易踩的坑(背下来少加班)

解决口诀
全局变量在进程池不共享传参、队列或 multiprocessing.Manager
进程池里抛异常会 Brokentry/except BrokenProcessPool
submitshutdownwith ThreadPoolExecutor() as ex:

四、总结背诵清单

  1. 线程池ThreadPoolExecutor(max_workers=n)
  2. 进程池ProcessPoolExecutor(max_workers=n)
  3. 批量 mapexecutor.map(func, iterable) → 顺序固定
  4. 单个 submitfuture = executor.submit(func, *args)
  5. 谁先完成for f in as_completed(futures): ...
  6. 回调future.add_done_callback(fn)
  7. 取消future.cancel()
  8. 异常future.exception() / try...except BrokenProcessPool
  9. 上下文管理with executor: 自动 shutdown
http://www.lryc.cn/news/587325.html

相关文章:

  • 17.使用DenseNet网络进行Fashion-Mnist分类
  • 2024CVPR:Question Aware Vision Transformer for Multimodal Reasoning介绍
  • Action-Agnostic Point-Level Supervision for Temporal Action Detection
  • 【读书笔记】《Effective Modern C++》第4章 Smart Pointers
  • 从零开始学习深度学习—水果分类之PyQt5App
  • gcc 源码阅读--C语言预处理
  • 深度学习16(对抗生成网络:GAN+自动编码器)
  • 深入理解 Java JVM
  • Java: OracleHelper
  • MYSQL笔记2
  • 线性基学习笔记
  • 查看Linux服务器显卡使用情况的详细教程
  • 【UE教程/进阶】使用Slate
  • 【unitrix】 5.0 第二套类型级二进制数基本结构体(types2.rs)
  • SQL预编译:安全高效数据库操作的关键
  • 苍穹外卖Day3
  • markdown-it-mathjax3-pro —— 新一代 Markdown 数学公式渲染插件
  • vue的优缺点
  • 框架和库的区别
  • day16~17-系统负载高故障与磁盘管理
  • muduo概述
  • 电商系统未来三年趋势:体验升级、技术赋能与模式重构
  • ASP.NET Core 中的延迟注入:原理与实践
  • 【UE教程/进阶】UE中的指针与引用
  • 应用层协议和JSON的使用
  • gcc 源码阅读---程序入口
  • 面试150 从前序与中序遍历构造二叉树
  • python赤道上空的大气环流剖面图(纬向-高度剖面)
  • Node.js 聊天内容加密解密实战教程(含缓存密钥优化)
  • 【elementUI踩坑记录】解决 el-table 固定列 el-table__fixed 导致部分滚动条无法拖动的问题