我想要做的是將所有 JobHandles 保存在一個向量中,并在迭代 X 時間后我想等待它們。
我這樣做是因為如果我在特定時間范圍內發送太多請求,我發送請求的端點也會回傳 429 錯誤。
#[tokio::main]
pub async fn get_collection_stats(city_name: String) -> Result<(serde_json::Value, surf::StatusCode), Box<dyn Error>> {
let endpoint = format!("https://some-city-api.org/{}", city_name);
let mut city_res = surf::get(&endpoint).await;
let mut res: surf::Response= match city_res {
Ok(value) => value,
Err(e) => { panic!("Error: {}", e) }
};
let stats: serde_json::Value = match res.body_json().await.ok() {
Some(val) => val,
None => serde_json::from_str("{}").unwrap()
};
Ok((stats, res.status()))
}
let mut count = 0;
let mut requests: Vec<_> = Vec::new();
for name in city_names {
if count < 5 {
let mut stats = tokio::task::spawn_blocking(|| {
match get_data_about_city(String::from(name)) {
Ok(value) => value,
Err(_) => serde_json::from_str("{}").unwrap()
}
});
requests.push(stats);
count = 1;
} else {
for task in requests {
dbg!(task.await);
}
count = 0;
break
}
}
到目前為止,我有這個。這很好用,但只有當我在 else 中有中斷時才有效。我希望能夠在沒有中斷的情況下批量處理 5 個請求。如果沒有休息,我會收到這樣的錯誤:
error[E0382]: borrow of moved value: `requests`
--> src\main.rs:109:13
|
87 | let mut requests: Vec<_> = Vec::new();
| ------------ move occurs because `requests` has type `Vec<tokio::task::JoinHandle<(serde_json::Value, StatusCode)>>`, which does not implement the `Copy` trait
...
109 | requests.push(stats);
| ^^^^^^^^^^^^^^^^^^^^ value borrowed here after move
...
112 | for task in requests {
| --------
| |
| `requests` moved due to this implicit call to `.into_iter()`, in previous iteration of loop
| help: consider borrowing to avoid moving into the for loop: `&requests`
|
note: this function takes ownership of the receiver `self`, which moves `requests`
--> C:\Users\Zed\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\iter\traits\collect.rs:234:18
|
234 | fn into_iter(self) -> Self::IntoIter;
| ^^^^
好的,我解決了移動問題。現在我有這個問題。
|
113 | dbg!(task.await);
| ^^^^^^^^^^ `&tokio::task::JoinHandle<(serde_json::Value, StatusCode)>` is not a future
|
= help: the trait `Future` is not implemented for `&tokio::task::JoinHandle<(serde_json::Value, StatusCode)>`
= note: `Future` is implemented for `&mut tokio::task::JoinHandle<(serde_json::Value, surf::StatusCode)>`, but not for `&tokio::task::JoinHandle<(serde_json::Value, surf::StatusCode)>`
我應該如何繼續我想做的事情?
uj5u.com熱心網友回復:
我想到了。我最終使用了期貨箱。流程看起來像這樣。
for name in city_names {
let url = format!("https://some-city-api.org/{}", name);
urls.push(url.clone());
}
let mut futs = FuturesUnordered::new();
for url in urls {
futs.push(surf::get(url));
// 50 requests reached, await everything in buffer
if futs.len() == 50 {
while let Some(res) = futs.next().await {
// Do something with requests
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/440242.html
