How to limit concurrency with Python asyncio?

后端 未结 5 1228
我在风中等你
我在风中等你 2020-11-27 02:35

Let\'s assume we have a bunch of links to download and each of the link may take a different amount of time to download. And I\'m allowed to download using utmost 3 connecti

5条回答
  •  孤城傲影
    2020-11-27 03:30

    Small Update: It's no longer necessary to create the loop. I tweaked the code below. Just cleans things up slightly.

    # download(code) is the same
    
    async def main():
        no_concurrent = 3
        dltasks = set()
        for i in range(9):
            if len(dltasks) >= no_concurrent:
                # Wait for some download to finish before adding a new one
                _done, dltasks = await asyncio.wait(dltasks, return_when=asyncio.FIRST_COMPLETED)
            dltasks.add(asyncio.create_task(download(i)))
        # Wait for the remaining downloads to finish
        await asyncio.wait(dltasks)
    
    if __name__ == '__main__':
        asyncio.run(main())
    

提交回复
热议问题