tokio
https://tokio-zh.github.io/
https://github.com/tokio-rs/tokio
https://tokio.rs/
https://docs.rs/tokio/0.2.18/tokio/
| [dependencies]
tokio = { version = "0.2", features = ["full"] }
chrono = "*"
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 | use tokio::prelude::*;
use core::time::Duration;
use chrono::prelude::*;
#[tokio::main]
async fn main() {
println!("time = {}", Local::now());
tokio::spawn(async {
tokio::time::delay_for(Duration::from_secs(3)).await;
println!("await 1");
println!("1 time = {}", Local::now());
});
tokio::spawn(async {
tokio::time::delay_for(Duration::from_secs(4)).await;
println!("await 2");
println!("2 time = {}", Local::now());
});
tokio::time::delay_for(Duration::from_secs(5)).await;
println!("finish");
println!("3 time = {}", Local::now());
}
// time = 2020-04-17 16:55:49.956206 +08:00
// await 1
// 1 time = 2020-04-17 16:55:52.963132 +08:00
// await 2
// 2 time = 2020-04-17 16:55:53.959542 +08:00
// finish
// 3 time = 2020-04-17 16:55:54.959682 +08:00
|
| [dependencies]
tokio = { version = "0.2", features = ["full"] }
chrono = "*"
actix-rt = "1.0.0"
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 | use tokio::prelude::*;
use core::time::Duration;
use chrono::prelude::*;
// #[tokio::main]
#[actix_rt::main]
async fn main() {
println!("time = {}", Local::now());
tokio::spawn(async {
tokio::time::delay_for(Duration::from_secs(3)).await;
println!("await 1");
println!("1 time = {}", Local::now());
});
tokio::spawn(async {
tokio::time::delay_for(Duration::from_secs(4)).await;
println!("await 2");
println!("2 time = {}", Local::now());
});
tokio::time::delay_for(Duration::from_secs(5)).await;
println!("finish");
println!("3 time = {}", Local::now());
}
// time = 2020-04-17 17:04:14.306841 +08:00
// await 1
// 1 time = 2020-04-17 17:04:17.312013 +08:00
// await 2
// 2 time = 2020-04-17 17:04:18.312003 +08:00
// finish
// 3 time = 2020-04-17 17:04:19.310049 +08:00
|