用途:该技能面向 Rust 异步编程,提供基于 Tokio 运行时的生产级模式,适合构建异步 Rust 应用、实现并发网络服务、使用 Tokio 做异步 I/O、处理异步错误、调试异步代码及优化异步性能。
主要能力:核心概念包括 Future(惰性计算)、async fn、await、Task、Runtime,以及 poll/Waker 调度模型。涵盖的模式有:并发任务执行(JoinSet、buffer_unordered 限流、select! 竞速);通道通信(mpsc、broadcast、oneshot、watch);异步错误处理(anyhow、thiserror、超时封装);优雅关闭(CancellationToken、broadcast 关闭信号);异步 trait(async-trait 定义 Repository 及其实现);流与异步迭代(async_stream、StreamExt、分块、合并流);资源管理(RwLock、Mutex、Semaphore 连接池)。还包含调试技巧(tokio-console、tracing::instrument、span)与最佳实践。
使用方式:依赖示例为 tokio = { version = "1", features = ["full"] }、futures = "0.3"、async-trait = "0.1"、anyhow = "1.0"、tracing = "0.1"、tracing-subscriber = "0.3";使用 #[tokio::main] 入口。调试可设 RUSTFLAGS="--cfg tokio_unstable" cargo run 并运行 tokio-console。
限制与最佳实践:不要阻塞(避免 std::thread::sleep)、不要跨 await 持有锁、避免无界 spawn(用信号量限流)、不要忽略错误、为 spawn 的 Future 注意 Send 约束。具体版本、价格与平台信息暂未提供。