first commit

This commit is contained in:
2026-06-12 14:02:15 +08:00
commit 9cbdc1d95d
69 changed files with 9486 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
"""Async helpers for executing bounded concurrent workloads."""
from __future__ import annotations
import asyncio
from typing import Awaitable, Callable, TypeVar
T = TypeVar("T")
async def gather_with_limit(
factories: list[Callable[[], Awaitable[T]]],
limit: int,
) -> list[T]:
"""Run async factory callables with a maximum concurrency limit."""
semaphore = asyncio.Semaphore(max(1, limit))
async def guarded(factory: Callable[[], Awaitable[T]]) -> T:
"""Wrap one factory invocation with semaphore-based throttling."""
async with semaphore:
return await factory()
return await asyncio.gather(*(guarded(factory) for factory in factories))