Something went wrong. Try again.
Integrated repository, worktree, git stacker, and project manager
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172"""Small async utilities shared by concurrent fanouts.
`bounded_gather` is the one-stop replacement for the`asyncio.Semaphore + asyncio.TaskGroup` pattern that otherwise getsduplicated every time a subsystem wants a bounded fanout — see`repo.maintenance._maintain_async` and `stacker.render.prefetch.prefetch_all`.Both the structured concurrency (any child raising cancels siblings andsurfaces as `ExceptionGroup`) and the input-order-preserving result listare guarantees the callers depend on."""
from __future__ import annotations
import asynciofrom collections.abc import Awaitable, Callable, Iterablefrom typing import TypeVar
T = TypeVar("T")K = TypeVar("K")
async def bounded_gather( awaitables: Iterable[Awaitable[T]], *, limit: int,) -> list[T]: """Run awaitables concurrently with at most `limit` in flight.
Wraps each coroutine in a semaphore-acquiring shim and launches them in a `TaskGroup`, so a failure cancels siblings and surfaces as `ExceptionGroup`. Results come back in the same order as `awaitables`.
`limit <= 0` raises `ValueError` — unbounded callers don't need this helper; they can use `asyncio.gather` directly. Forcing callers to pass a positive cap keeps "I forgot to set a limit" from being a silent foot-gun. """ if limit <= 0: raise ValueError(f"limit must be >= 1, got {limit}") sem = asyncio.Semaphore(limit) coros = list(awaitables)
async def _bounded(coro: Awaitable[T]) -> T: async with sem: return await coro
async with asyncio.TaskGroup() as tg: tasks = [tg.create_task(_bounded(c)) for c in coros] return [t.result() for t in tasks]
async def bounded_gather_map( items: Iterable[K], fn: Callable[[K], Awaitable[T]], *, limit: int,) -> dict[K, T]: """`bounded_gather` convenience keyed on the input item.
Same semantics as `bounded_gather`, except the result is `{item: fn(item)}`. Useful when the caller wants to look results up later and would otherwise zip the input list against a positional result list. Duplicate items in the input collapse into one call — caller beware if that matters. """ item_list = list(items) results = await bounded_gather( (fn(item) for item in item_list), limit=limit, ) return dict(zip(item_list, results, strict=True))