add async/.await
This commit is contained in:
12
async_await/Cargo.toml
Normal file
12
async_await/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
[package]
|
||||||
|
name = "async_await"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
futures = "0.3.1"
|
||||||
|
isahc = "0.8.2"
|
||||||
|
http = "0.1.21"
|
||||||
24
async_await/src/main.rs
Normal file
24
async_await/src/main.rs
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
futures::executor::block_on(get_all());
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_all() {
|
||||||
|
let mut v = vec![];
|
||||||
|
for _ in 0..10 {
|
||||||
|
v.push(get_url("https://www.baidu.com/"));
|
||||||
|
}
|
||||||
|
|
||||||
|
futures::future::join_all(v).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_url(url: &str) {
|
||||||
|
println!("Start get: {}", url);
|
||||||
|
let r = isahc::get_async(url).await;
|
||||||
|
|
||||||
|
match r {
|
||||||
|
Err(e) => println!("Get response error: {}", e),
|
||||||
|
Ok(o) => println!("Response: {:?}", o.status()),
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user