如何解决我如何从select_all取回期货?
我需要下载很多文件,但是我想一次只处理3个文件。我正在从Tokio MPSC频道接收下一个文件URL。当我已经下载了2个文件并等待一个新文件时,我需要解决这种情况。
我如何等待3个期货中的一个并扩展前一个select_all
?
一个非常简化的示例:playground
use futures::{
future::{select,select_all,Either},FutureExt,};
use tokio::time::{delay_for,Duration};
async fn exec(val: usize) {
println!("Started {}",val);
delay_for(Duration::from_millis(100)).await;
println!("Executed {}",val);
}
async fn get_next() -> usize {
use rand::Rng;
delay_for(Duration::from_millis(100)).await;
rand::thread_rng().gen_range(0,10)
}
#[tokio::main]
async fn main() {
let mut tasks = Vec::new();
for _ in 0..10 {
if tasks.len() == 0 {
let next = get_next().await;
tasks.push(tokio::spawn(exec(next)));
continue;
}
let all = select_all(tasks.drain(..));
let next = get_next().boxed();
match select(all,next).await {
Either::Left(((_,_,left),_)) => tasks = left,Either::Right((a,_other)) => {
// How can I get tasks futures from 'other'?
// So on the next iteration,I'll be able to do select with an additional task.
tasks.push(tokio::spawn(exec(a)));
}
};
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。