0ioaths 最近的时间轴更新
0ioaths

0ioaths

V2EX 第 688025 号会员,加入于 2024-05-04 13:21:20 +08:00
0ioaths 最近回复了
33 天前
回复了 youngPacce 创建的主题 Rust rust 关于.await 的疑惑
将 `hello_cat` 和 `hello_dog` 作为异步任务执行就是期望的效果,以 `tokio runtime` 为例

```rust
use tokio::{runtime, time};

async fn hello_world() {
tokio::spawn(hello_cat());
tokio::spawn(hello_dog());

let five_secs = time::Duration::from_secs(5);
time::sleep(five_secs).await;
println!("hello, world!");
}

async fn hello_cat() {
println!("hello, kitty!");

let three_secs = time::Duration::from_secs(3);
time::sleep(three_secs).await;
println!("hello, kitty!2");
}

async fn hello_dog() {
println!("hello, snoopy!");
}

fn main(){
runtime::Builder::new_multi_thread()
.worker_threads(4)
.enable_all()
.build()
.unwrap()
.block_on(hello_world());
}

```

```shell
hello, kitty!
hello, snoopy!
hello, kitty!2 // after 3 sces
hello, world! // after 5 secs
```
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2361 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 12ms · UTC 10:46 · PVG 18:46 · LAX 03:46 · JFK 06:46
Developed with CodeLauncher
♥ Do have faith in what you're doing.