-
-
Notifications
You must be signed in to change notification settings - Fork 43
如何在同步的 Rust 方法中调用异步代码 - Tokio 使用中的几点教训 #98
-
如何在同步的 Rust 方法中调用异步代码 - Tokio 使用中的几点教训
在同步的 Rust 方法中调用异步代码经常会导致一些问题,特别是对于不熟悉异步 Rust runtime 底层原理的初学者。在本文中,我们将讨论我们遇到的一个特殊问题,并分享我们采取的解决方法的经验。
最近在做我们的 GreptimeDB 项目的时候遇到一个关于在同步 Rust 方法中调用异步代码的问题。经过一系列故障排查后,我们弄清了问题的原委,这大大加深了对异步 Rust 的理解,因此在这篇
https://rustmagazine.org/issue-3/bridging-async-and-sync-in-rust-zh/
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
Replies: 1 comment
-
futures::executor::block_on(
RUNTIME.spawn(async move {
let mut res = vec![];
for i in 0..bound {
res.push(i);
tokio::time::sleep(Duration::from_millis(100)).await;
}
res
})
).unwrap();
例子这样看上去更干净些
Beta Was this translation helpful? Give feedback.
All reactions
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment