Próbuję uruchomić dwie funkcje równolegle z join
.
Mój kod jest prosty:
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());
}
Ale co bym nie robił, to zajmie 4 godziny —2s + 2s
— w tym czasie jak to powinno trwać 2 sekundy, jeśli się nie mylę:
start: 37ns
end: 4.01036111s
Ja czegoś nie rozumiem?