Skip to content

concurrency

Thread-pool helper for I/O-bound work such as LLM API calls.

run_concurrently(items, worker, max_workers)

Yield (index, worker(item)) for each item as it finishes.

Results arrive in completion order; use the index to restore input order. Runs sequentially in input order when max_workers <= 1. Consume the results in the calling thread so that side effects (file writes, DataFrame updates, logging) stay single-threaded. If a worker raises, items not yet started are cancelled and the exception propagates to the caller.

Source code in src/ariadne/utils/concurrency.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def run_concurrently(
    items: Sequence[T], worker: Callable[[T], R], max_workers: int
) -> Iterator[tuple[int, R]]:
    """Yield ``(index, worker(item))`` for each item as it finishes.

    Results arrive in completion order; use the index to restore input order.
    Runs sequentially in input order when ``max_workers <= 1``. Consume the
    results in the calling thread so that side effects (file writes, DataFrame
    updates, logging) stay single-threaded. If a worker raises, items not yet
    started are cancelled and the exception propagates to the caller.
    """
    if max_workers <= 1 or len(items) <= 1:
        for idx, item in enumerate(items):
            yield idx, worker(item)
        return

    executor = ThreadPoolExecutor(max_workers=max_workers)
    try:
        futures = {executor.submit(worker, item): idx for idx, item in enumerate(items)}
        for future in as_completed(futures):
            yield futures[future], future.result()
    finally:
        executor.shutdown(wait=True, cancel_futures=True)