Skip to main content

run_concurrent_batch

Function run_concurrent_batch 

Source
pub async fn run_concurrent_batch<T, R, F>(
    items: Vec<T>,
    concurrency: usize,
    job_fn: &F,
) -> Vec<(R, bool)>
where T: Send + 'static, R: Send + 'static, F: Fn(T) -> Pin<Box<dyn Future<Output = (R, bool)> + Send>> + Send + Sync + Clone + 'static,
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 type
  • R - The result type
  • F - The function type that processes each item
  • Fut - The future type returned by the function

§Arguments

  • items - Vector of items to process
  • concurrency - Maximum number of concurrent operations
  • job_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;