我有一个设置,我的程序使用std::thread::spawn
.
我需要一个 GRPC 服务器来处理传入的命令以及工作线程完成的流输出。我正在使用tonic
GRPC 服务器,它只在 Tokio 未来提供异步实现。
我需要能够从我的“正常”标准库线程向 Tokio 未来发送消息。
我在这里将我的代码简化为最低限度:
use std::thread;
use tokio::sync::mpsc; // 1.9.0
fn main() {
let (tx, mut rx) = mpsc::channel(1);
let tokio_runtime = tokio::runtime::Runtime::new().unwrap();
tokio_runtime.spawn(async move {
// the code below starts the GRPC server in reality, here I'm just demonstrating trying to receive a message
while let Some(v) = rx.recv().await {}
});
let h = thread::spawn(move || {
// do work
tx.send(1).await; //<------ error occurs here since I can't await in a non-async block
});
h.join().unwrap();
}
我的主要工作线程如何与 Tokio 生成的 GRPC 服务器通信?