Is it possible to limit the number of coroutines running corcurrently in asyncio?

后端 未结 3 2179
臣服心动
臣服心动 2021-02-12 15:15

I already wrote my script using asyncio but found that the number of coroutines running simultaneously is too large and it often ends up hanging around.

So I would like

3条回答
  •  心在旅途
    2021-02-12 15:17

    You can wrap your gather and enforce a Semaphore:

    import asyncio
    
    async def semaphore_gather(num, coros, return_exceptions=False):
        semaphore = asyncio.Semaphore(num)
    
        async def _wrap_coro(coro):
            async with semaphore:
                return await coro
    
        return await asyncio.gather(
            *(_wrap_coro(coro) for coro in coros), return_exceptions=return_exceptions
        )
    
    # async def a():
    #     return 1
    
    # print(asyncio.run(semaphore_gather(10, [a() for _ in range(100)])))
    # [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
    

提交回复
热议问题