Skip to content

Commit e9d63a9

Browse files
committed
Updated sample code
1 parent d5cafed commit e9d63a9

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

Lesson_14/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ edition = "2024"
66
[dependencies]
77
tokio = { version = "1.45.0", features = ["full"] }
88
rand = "0.9.1"
9+
reqwest = "0.12.15"

Lesson_14/src/crawler.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use tokio::task;
2+
3+
#[allow(dead_code)]
4+
pub async fn run() {
5+
let task_a = task::spawn(fetch_data_async(
6+
"https://jsonplaceholder.typicode.com/posts/1",
7+
));
8+
let task_b = task::spawn(fetch_data_async(
9+
"https://jsonplaceholder.typicode.com/posts/2",
10+
));
11+
let task_c = task::spawn(fetch_data_async(
12+
"https://jsonplaceholder.typicode.com/posts/3",
13+
));
14+
15+
let (res_a, res_b, res_c) = tokio::join!(task_a, task_b, task_c);
16+
17+
match (res_a, res_b, res_c) {
18+
(Ok(a), Ok(b), Ok(c)) => {
19+
println!("{:?}", a);
20+
println!("{:?}", b);
21+
println!("{:?}", c);
22+
}
23+
_ => println!("Failed to fetch data"),
24+
}
25+
}
26+
27+
async fn fetch_data_async(url: &str) -> Result<String, reqwest::Error> {
28+
let response = reqwest::get(url).await?;
29+
response.text().await
30+
}

Lesson_14/src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
mod crawler;
2+
13
use rand::Rng;
24
use std::time::Duration;
35
use tokio::sync::mpsc;
@@ -18,6 +20,8 @@ async fn main() {
1820
});
1921

2022
let _ = tokio::join!(cpu_task, memory_task, disk_task, logger_task);
23+
24+
// crawler::run().await;
2125
}
2226

2327
async fn fetch_metrics(service_name: &str, tx: mpsc::Sender<String>) {

0 commit comments

Comments
 (0)