pub async fn run_concurrent_batch<T, R, F>(
items: Vec<T>,
concurrency: usize,
job_fn: &F,
) -> Vec<(R, bool)>Expand description
Run a batch of operations concurrently with limited parallelism.
This function takes a collection of items, a concurrency limit, and a job function. It processes the items concurrently but limited to the specified level of parallelism, returning the results when all operations are complete.
§Type Parameters
T- The input item typeR- The result typeF- The function type that processes each itemFut- The future type returned by the function
§Arguments
items- Vector of items to processconcurrency- Maximum number of concurrent operationsjob_fn- Function that processes each item and returns a future
§Returns
A vector containing the results of all operations in the same order as the input items.
§Examples
async fn process_item(item: u32) -> u32 {
// Some async processing
item * 2
}
let items = vec![1, 2, 3, 4, 5];
let concurrency = 2;
let results = run_concurrent_batch(items, concurrency, |item| async move {
process_item(item).await
}).await;