3.1 异步通道
01-异步通道 — Comprehensive Rust
译文 · 基于 Comprehensive Rust
原文链接: https://google.github.io/comprehensive-rust/concurrency/async-control-flow/channels.html
3.1 异步通道
多个 crate 支持异步通道。例如 tokio:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| // Copyright 2024 Google LLC
// SPDX-License-Identifier: Apache-2.0
use tokio::sync::mpsc;
async fn ping_handler(mut input: mpsc::Receiver<()>) {
let mut count: usize = 0;
while let Some(_) = input.recv().await {
count += 1;
println!("Received {count} pings so far.");
}
println!("ping_handler complete");
}
#[tokio::main]
async fn main() {
let (sender, receiver) = mpsc::channel(32);
let ping_handler_task = tokio::spawn(ping_handler(receiver));
for i in 0..10 {
sender.send(()).await.expect("Failed to send ping.");
println!("Sent {} pings so far.", i + 1);
}
drop(sender);
ping_handler_task.await.expect("Something went wrong in ping handler task.");
}
|
把通道容量改成 3,观察对执行的影响。
总体而言,接口与上午课程中的 sync 通道类似。
试着去掉 std::mem::drop 调用。会发生什么?为什么?
Flume crate 的通道同时实现了 sync 与 async 的 send 与 recv。对于既有 IO 又有繁重 CPU 处理的复杂应用,这会很方便。
使用 async 通道更可取之处在于:可以把它们与其他 future 组合,构建复杂控制流。