Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

English Original

总结与备忘卡 (Reference Card) 🟡

快速参考卡

异步思维模型

┌─────────────────────────────────────────────────────┐
│  async fn → 状态机 (enum) → 实现 Future Trait        │
│  .await   → 调用内部 future 的 poll() 方法           │
│  执行器    → 循环 { poll(); 睡眠直至被唤醒; }         │
│  Waker    → “嘿,执行器,再次轮询我”                 │
│  Pin      → “我承诺不会在内存中移动位置”             │
└─────────────────────────────────────────────────────┘

常见模式速查表

目标方法
并发运行两个 futuretokio::join!(a, b)
竞速运行两个 futuretokio::select! { ... }
派生一个后台任务tokio::spawn(async { ... })
在异步中运行阻塞代码`tokio::task::spawn_blocking(
限制并发数量Semaphore::new(N)
收集多个任务的结果JoinSet
跨任务共享状态Arc<Mutex<T>> 或使用通道
优雅停机watch::channel + select!
每次并发处理 N 个流条目.buffer_unordered(N)
为 future 设置超时tokio::time::timeout(dur, fut)
带退避算法的重试自定义组合器 (见第 13 章)

固定 (Pinning) 快速参考

场景方法
在堆上固定 futureBox::pin(fut)
在栈上固定 futuretokio::pin!(fut)
固定一个 Unpin 类型Pin::new(&mut val) —— 安全且零开销
返回固定的 trait 对象-> Pin<Box<dyn Future<Output = T> + Send>>

通道 (Channel) 选择指南

通道生产者消费者传输内容适用场景
mpsc多个 (N)1工作队列、事件总线
oneshot11单个值请求/响应模式、完成通知
broadcast多个 (N)多个 (N)所有人收到所有扇出通知、停机广播
watch1多个 (N)仅最新值配置更新、健康状态检查

Mutex 选择指南

Mutex适用场景
std::sync::Mutex持锁时间极短,决不跨越 .await
tokio::sync::Mutex必须跨越 .await 点持有锁
parking_lot::Mutex高并发竞争,无 .await,极致性能
tokio::sync::RwLock多读少写,且需要跨越 .await

决策快速参考

需要并发?
├── I/O 密集型 → 使用 async/await
├── CPU 密集型 → 使用 rayon / std::thread
└── 混合型 → 为 CPU 部分使用 spawn_blocking

选择运行时?
├── 服务器应用 → 使用 tokio
├── 类库项目 → 运行时无关设计 (使用 futures 包)
├── 嵌入式项目 → 使用 embassy
└── 极简项目 → 使用 smol

需要并发运行 Future?
├── 满足 'static + Send → 使用 tokio::spawn
├── 满足 'static + !Send → 使用 LocalSet
├── 不满足 'static → 使用 FuturesUnordered
└── 需要追踪/中止任务 → 使用 JoinSet

常见错误信息及修复方案

错误信息原因修复方法
future is not Send.await 持有了 !Send 类型收窄锁作用域或使用 current_thread
borrowed value does not live long enough (在 spawn 中)tokio::spawn 要求 'static 生命周期使用 Arc、克隆或 FuturesUnordered
the trait Future is not implemented for ()遗漏了 .await在异步调用后补上 .await
cannot borrow as mutable (在 poll 中)自引用借用问题正确使用 Pin<&mut Self> (见第 4 章)
程序发生静默挂起忘记调用 waker.wake()确保每个 Pending 路径都注册了 waker

延伸阅读

资源推荐理由
Tokio 官方教程官方出品 —— 入门首选指南
Async Book (官方文档)从语言层面涵盖 Future, Pin, Stream
Jon Gjengset —— Crust of Rust: async/await2 小时带源码实测深度解析
Alice Ryhl —— 使用 Tokio 构建 Actor生产级有状态服务的架构模式
Without Boats —— Pin, Unpin, 及其设计初衷核心设计者的原始设计逻辑
Tokio mini-Redis完整的异步项目 —— 极具参考价值的代码库
Tower 官方文档axum/tonic 使用的中间件/服务模式