Rust 并行下载不执行链式异步调用

问题描述 投票:0回答:1

我一直在努力使这个

store_result
在此方法链中执行:

let (tasks, files): (Vec<_>, Vec<_>) = result_files
    .into_iter()
    .filter(|x| should_download_file(&provider_id_path, x.filename.clone()))
    .map(download_file)
    .unzip();

let results = future::join_all(tasks).await;

let store_result = |x: (Result<Response, Error>, Result<File, std::io::Error>)| async {
    //info!("Storing result: {:?}, {:?}", x.0, x.1);

    // make sure both are Oks
    let (result, file) = match (x.0, x.1) {
        (Ok(result), Ok(file)) => {
            info!("Got result and file.");
            info!("Result: {:?}", result.url());
            info!("File: {:?}", file);
            (result, file)
        }
        _ => {
            error!("Failed to get result and file.");
            return;
        }
    };
    // Write bytes to file
    let _ = write_bytes_to_file(file, result).await;
};

// Map result and file to store_result
let _ = results
    .into_iter()
    .zip(files)
    .map(|x| async {
      debug!("Storing result: {:?}, {:?}", x.0, x.1);
        let _ = store_result(x).await;
    })
    .collect::<Vec<_>>();

info!("Batch download done.");

future::join_all(tasks).await;
已正确执行。代码有什么问题吗?

asynchronous rust async-await rust-futures
1个回答
0
投票

我以前经历过这个,但不知何故我错过了:

// Map result and file to store_result
let store_results = results
    .into_iter()
    .zip(files)
    .map(|x| async {
        debug!("Storing result: {:?}, {:?}", x.0, x.1);
        let _ = store_result(x).await;
    })
    .collect::<Vec<_>>();

let _ = future::join_all(store_results).await;
© www.soinside.com 2019 - 2024. All rights reserved.