# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/utils/execution.ipynb.

# %% auto 0
__all__ = ['gather_with_concurrency']

# %% ../../nbs/utils/execution.ipynb 3
import asyncio

# %% ../../nbs/utils/execution.ipynb 5
async def gather_with_concurrency(
    *coros,  # list of coroutines to await
    n=60,  # number of open coroutines
):
    """limits the number of open coroutines at a time."""

    semaphore = asyncio.Semaphore(n)

    async def sem_coro(coro):
        async with semaphore:
            return await coro

    return await asyncio.gather(*(sem_coro(c) for c in coros))
