← [ BACK_TO_NOTES ]

Python 并发与异步 MOC

线程、进程、GIL、Executor 与 asyncio 的阶段导航;先分清工作负载、调度层次与资源边界,再选择 native async、thread 或 process。

#moc #growing #dev #python #python

[!info] related notes

Python 并发与异步 MOC

知识总览

filters:
  and:
    - 'file.ext == "md"'
    - 'file.inFolder("z")'
    - 'this.file.links.contains(file)'
    - 'file.path != this.file.path'
formulas:
  knowledge: 'file.asLink(title)'
properties:
  formula.knowledge:
    displayName: 知识
  note.description:
    displayName: Description
  note.mastery:
    displayName: Mastery
views:
  - type: table
    name: 知识总览
    order:
      - formula.knowledge
      - note.description
      - note.mastery
    columnSize:
      formula.knowledge: 280
      note.description: 760
      note.mastery: 56

从这里开始

推荐按“先分清模型 → 再理解执行资源 → 最后进入 asyncio 生命周期”的顺序:

  1. 并发模型总览
  2. GIL 与线程
  3. Executor、Thread、Process 与 asyncio.to_thread
  4. 多进程与进程池
  5. 异步编程
  6. 任务、超时与取消
  7. 同步原语与队列

一张选型地图

任务会明显耗时吗?
│
├─ 否 → 直接同步执行
│
└─ 是
   │
   ├─ 大部分时间在等待 I/O?
   │   ├─ 有 native async API → asyncio / await
   │   └─ 只有 sync API → ThreadPool / asyncio.to_thread
   │
   └─ 大部分时间在计算?
       ├─ native library / 可能释放 GIL → bounded thread + benchmark
       └─ heavy pure Python → ProcessPool / worker

同时记住三层调度:

Coroutine → Event Loop 协作式调度
Thread / Process → OS 抢占式调度
Python bytecode on traditional CPython Threads → 还受 GIL 约束

笔记列表

真实项目应用

继续深入

创建于 2026/7/15 更新于 2026/10/17
Digital Biome
Access

键盘导航

j / k / ↑ / ↓
选择对象
Enter / o
打开选择
/ 或 Ctrl+K
全站搜索
i / f / 1–4
筛选当前列表
g → h / l / p / t / a
首页 / 笔记 / 项目 / 工具 / About
r → 1–9
打开所选对象的关联
u
返回上一页
Esc
关闭浮层 / 取消命令;输入框内清空文本