我试图运行两个平行功能 join
.
我的代码是简单的:
tokio = { version = "1.14.0", features = ["full"] }
use tokio::join;
use std::thread::sleep;
use std::time::{Duration, Instant};
async fn fn_1() -> i8 {
sleep(Duration::from_secs(2));
2
}
async fn fn_2() -> i8 {
sleep(Duration::from_secs(2));
1
}
#[tokio::main]
async fn main() -> () {
let now = Instant::now();
println!("start: {:#?}", now.elapsed());
let a = fn_1();
let b = fn_2();
join!(a, b);
println!("end: {:#?}", now.elapsed());
}
但无论我做什么,这需要4s—2s + 2s
—,同时它应该采取2s如果我没有记错的话:
start: 37ns
end: 4.01036111s
有什么东西是我的失踪?