入门
https://learnku.com/docs/rust-async-std
https://learnku.com/docs/async-book/2018
https://docs.rs/async-std/1.5.0/async_std/index.html
https://github.com/async-rs/async-std
https://async.rs/
Hello World
Cargo.toml
:
| [dependencies]
async-std = "*"
futures = "*"
chrono = "*"
|
src/main.rs
:
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 | use async_std::task;
use std::time::Duration;
use chrono::Local;
async fn hello_world1() {
task::sleep(Duration::from_secs(2)).await;
println!("hello world1!, {}", Local::now());
}
async fn hello_world2() {
task::sleep(Duration::from_secs(3)).await;
println!("hello world2!, {}", Local::now());
}
async fn say_hello() {
futures::join!(hello_world1(), hello_world2());
}
fn main() {
println!("start time = {}", Local::now());
task::block_on(say_hello());
}
// start time = 2020-04-07 16:28:03.138334 +08:00
// hello world1!, 2020-04-07 16:28:05.142119 +08:00
// hello world2!, 2020-04-07 16:28:06.141368 +08:00
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 | use async_std::task;
use std::time::Duration;
use chrono::Local;
async fn hello_world1() {
task::sleep(Duration::from_secs(2)).await;
println!("hello world1!, {}", Local::now());
}
async fn hello_world2() {
task::sleep(Duration::from_secs(3)).await;
println!("hello world2!, {}", Local::now());
}
fn main() {
println!("start time = {}", Local::now());
task::block_on(async {
futures::join!(hello_world1(), hello_world2());
});
}
// start time = 2020-04-07 16:38:09.248546 +08:00
// hello world1!, 2020-04-07 16:38:11.249414 +08:00
// hello world2!, 2020-04-07 16:38:12.251518 +08:00
|
使用task::spawn
新建异步任务
Cargo.toml
:
| [dependencies]
async-std = "*"
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 | use async_std::task;
use std::time::Duration;
use chrono::Local;
async fn hello_world1() {
task::sleep(Duration::from_secs(2)).await;
println!("hello world1!, {}", Local::now());
}
async fn hello_world2() {
task::sleep(Duration::from_secs(3)).await;
println!("hello world2!, {}", Local::now());
}
async fn say_hello() {
task::spawn(hello_world1());
hello_world2().await;
}
fn main() {
println!("start time = {}", Local::now());
task::block_on(say_hello());
}
// start time = 2020-04-08 10:18:32.088184 +08:00
// hello world1!, 2020-04-08 10:18:34.093211 +08:00
// hello world2!, 2020-04-08 10:18:35.093221 +08:00
|
| async-std = { version = "1.8.0", features = ["attributes"] }
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | use async_std::task;
use async_std::task::sleep;
use std::time::Duration;
async fn test() {
sleep(Duration::from_secs(3)).await;
println!("hhh");
}
#[async_std::main]
async fn main() {
task::spawn(test());
sleep(Duration::from_secs(5)).await;
}
|
输出结果:
异步锁
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
30
31
32
33
34
35 | use async_std::task;
use async_std::sync::{Arc, Mutex};
use chrono::prelude::*;
use std::time::Duration;
use async_std::task::sleep;
async fn run() {
let val = Arc::new(Mutex::new(0));
for _ in 0..100 {
let v_t = val.clone();
task::spawn(async move {
let mut v = v_t.lock().await;
// sleep(Duration::from_secs(2)).await;
(*v) += 1;
println!("{}, {}", Local::now(), *v);
});
}
sleep(Duration::from_secs(300)).await;
}
fn main() {
task::block_on(run());
}
// 2020-08-15 09:31:01.753957646 +08:00, 1
// 2020-08-15 09:31:01.754034511 +08:00, 2
// 2020-08-15 09:31:01.754077526 +08:00, 3
// 2020-08-15 09:31:01.754130453 +08:00, 4
// 2020-08-15 09:31:01.754190871 +08:00, 5
// 2020-08-15 09:31:01.754314989 +08:00, 6
// 2020-08-15 09:31:01.754371709 +08:00, 7
// ...
// 2020-08-15 09:31:01.761088155 +08:00, 97
// 2020-08-15 09:31:01.761135372 +08:00, 98
// 2020-08-15 09:31:01.761214170 +08:00, 99
// 2020-08-15 09:31:01.761266422 +08:00, 100
|
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
30 | use async_std::task;
use async_std::sync::{Arc, Mutex};
use chrono::prelude::*;
use std::time::Duration;
use async_std::task::sleep;
async fn run() {
let val = Arc::new(Mutex::new(0));
for _ in 0..100 {
let v_t = val.clone();
task::spawn(async move {
// 只有当 v 被 drop 之后才会释放锁
let mut v = v_t.lock().await;
sleep(Duration::from_secs(2)).await;
(*v) += 1;
println!("{}, {}", Local::now(), *v);
});
}
sleep(Duration::from_secs(300)).await;
}
fn main() {
task::block_on(run());
}
// 2020-08-15 09:31:55.321214422 +08:00, 1
// 2020-08-15 09:31:57.322968617 +08:00, 2
// 2020-08-15 09:31:59.324643660 +08:00, 3
// 2020-08-15 09:32:01.326317138 +08:00, 4
// 2020-08-15 09:32:03.327969868 +08:00, 5
// ...
|
手动释放锁:
| let mut v = v_t.lock().await;
drop(v);
|
使用原子类型
| async-std = { version = "1.9.0", features = ["attributes", "unstable"] }
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | use std::{
sync::atomic::{AtomicI64, Ordering},
sync::Arc,
};
use async_std::task;
#[async_std::main]
async fn main() {
let x = AtomicI64::new(2333);
let arc_x = Arc::new(x);
let arc_x_clone = Arc::clone(&arc_x);
task::spawn(async move {
arc_x_clone.store(6666, Ordering::Relaxed);
});
println!("{}", arc_x.load(Ordering::Relaxed));
async_std::task::sleep(std::time::Duration::from_secs(3)).await;
println!("{}", arc_x.load(Ordering::Relaxed));
}
|
输出结果: