读书笔记 · 约 13,514 字 · 预计推送 2 部分

主题精读:并发与异步编程——线程、进程、async/await、Actor、消息队列的区别 未开始

目录

题记与说明


一、七维提取

1. 核心问题:这份"主题"在解决什么问题

五种东西经常被并排摆在"并发"这个词下,但它们其实分属三个不同层次,回答的是三个不同的问题:

所以"区别"不是一个五选一的选择题:前三个是互斥的执行模型(一个进程内总要选一种主模型),Actor 与消息队列是建立在前三者之上的通信与架构模式。真正的决策链是"先定执行模型,再定通信模型"。

2. 主要主张(各来源的核心论点)

来源核心主张原文关键句(摘引)
Akka《Actor Systems》Actor 是"最严格的面向对象":状态与行为封装,只通过邮箱通信;用层级与监督处理失败,而不是防御式编程"Actors are objects which encapsulate state and behavior, they communicate exclusively by exchanging messages which are placed into the recipient's mailbox." / "tasks are split up and delegated until they become small enough to be handled in one piece"
Microsoft Learn《Asynchronous programming》async/await 的目标是"读起来像顺序语句,执行顺序却复杂得多";await 暂停的是方法,不是线程;异步≠并行(一个人做早餐 vs 多个厨师)"The goal of task asynchronous programming is to enable code that reads like a sequence of statements, but executes in a more complicated order." / "Cooking breakfast is a good example of asynchronous work that isn't parallel. One person (or thread) can handle all the tasks."
Real Python《Async IO》asyncio 是单线程、单进程的协作式多任务;对 I/O 密集任务优于线程(无线程管理开销),但对 CPU 密集毫无用处;线程是"有限资源",协程不是"Creating thousands of threads will fail on many machines or can slow down your code." / "Only use async def functions for non-blocking operations."
AWS《What is a Message Queue?》消息队列是"服务到服务的异步通信":broker 是临时存消息的轻量缓冲,一条消息只被一个消费者处理一次(点对点),广播要叠加 Pub/Sub"Messages are stored on the queue until they are processed and deleted." / "Many producers and consumers can use the queue, but each message is processed only once, by a single consumer."
Chris Kiehl《Parallelism in One Line》Python 并行问题更多是教学问题而非技术问题:CPU 密集用 multiprocessing.Pool.map,I/O 密集用 multiprocessing.dummy(线程版),一行 map 注入并行"If you run too many threads, you'll waste more time switching between them than doing useful work." / "Map is a cool little function, and the key to easily injecting parallelism into your Python code."

3. 方法:这个主题的"论证方式"是什么

4. 数据与证据

5. 结论:一张坐标系里的五个位置

维度进程线程async/awaitActor消息队列
谁在调度OS(抢占式)OS(抢占式)事件循环(协作式,await 是让位点)Actor 运行时(邮箱驱动,底层仍是线程/事件循环)Broker(存储转发,不调度计算)
状态归属独立地址空间,天然隔离全体共享,必须加锁单线程内共享,await 点是竞争窗口Actor 私有,外部不可见("不要在 actor 间传可变对象")无状态;状态随消息走或落在下游服务
通信机制管道/共享内存/socket共享内存+锁对象引用/Future不可变消息 → 邮箱(ask/tell)持久化消息 → 队列(至多一次/至少一次)
单位成本最高(fork + IPC)中(MB 级栈)极低(KB 级协程帧)低(~300 字节/actor)最高(跨网络,持久化+确认)
失败影响一进程崩不影响其他一线程崩可能拖垮整个进程协程异常需在 await 处捕获(.NET:异常存入 Task,await 时重抛)失败被监督者捕获,"错误内核"模式:把危险子任务下放给子 actor消费失败可重试/进死信;生产者消费者互不拖垮
典型量级~CPU 核数数百数万–数十万百万级跨服务,百万级消息/天
最佳场景CPU 密集并行、故障隔离调阻塞库(不 await 的 DB 驱动等)、中等并发 I/O单机高并发 I/O(爬虫、网关、SSE/WS)有状态并发、容错分布式(游戏、IM、交易)服务解耦、削峰、跨机/跨语言任务分发

决策链(本次精读的收敛结论):

  1. 任务是 CPU 密集?→ 进程池(multiprocessing.Pool),核数= worker 数。
  2. I/O 密集但依赖阻塞库、并发 <1k?→ 线程池。
  3. I/O 密集、全链路可用非阻塞库、并发上万?→ async/await(记住:链上任何一个阻塞调用"stops everything in its tracks")。
  4. 并发单元持有可变状态且要容错?→ Actor(用消息传递消灭锁)。
  5. 发送方和接收方不在一个进程/一台机器,或需要削峰重试?→ 消息队列。
  6. 它们是可组合的:async 进程内处理 I/O + 进程池跑 CPU + 队列连接服务 + Actor 管有状态会话,是生产系统的常见组合,不是单选题。

6. 局限(来源承认的 + 本次研究推断的)

7. 对我的启发


二、逐来源精读摘录(原文引述)

来源 1:Akka 官方文档《Actor Systems》(全文精读)

"Actors are objects which encapsulate state and behavior, they communicate exclusively by exchanging messages which are placed into the recipient's mailbox. In a sense, actors are the most stringent form of object-oriented programming, but it serves better to view them as persons: while modeling a solution with actors, envision a group of people and assign sub-tasks to them, arrange their functions into an organizational structure and think about how to escalate failure."

要点:

来源 2:Microsoft Learn《Asynchronous programming - C#》(全文精读)

"Cooking breakfast is a good example of asynchronous work that isn't parallel. One person (or thread) can handle all the tasks. One person can make breakfast asynchronously by starting the next task before the previous task completes. ... For a parallel algorithm, you need multiple people who cook (or multiple threads)."

要点:

来源 3:Real Python《Async IO in Python》(深度提取精读)

"Asynchronous routines can pause their execution while waiting for a result and allow other routines to run in the meantime." / "Creating thousands of threads will fail on many machines or can slow down your code."

要点:

来源 4:AWS《What is a Message Queue?》(深度提取精读)

"A message queue is a form of asynchronous service-to-service communication used in serverless and microservices architectures." / "Many producers and consumers can use the queue, but each message is processed only once, by a single consumer."

要点:

来源 5:Chris Kiehl《Parallelism in One Line》(深度提取精读)

"If you run too many threads, you'll waste more time switching between them than doing useful work." / "Map is a cool little function, and the key to easily injecting parallelism into your Python code."

要点:


三、检索与验证记录(本会话实际执行)

四、相关链接(检索所得、旁证级)


*精读方法说明:本笔记基于 5 个已精读来源 + 3 轮检索摘要撰写;所有引述均来自上述链接页面(全文抓取或深度提取)。未在本机复跑任何性能数字;Go/Rust/Erlang 生态未做第一手精读,已在局限中标注。*

← 资料库今日播报 →