将异步函数应用于具有有限并发性的数组或队列中的每个项
aioeachlimit的Python项目详细描述
aioeachlimit
以有限的并发性将异步函数应用于数组或队列中的每个项
安装
pip install aioeachlimit
用法
async def f(item):
asyncio.sleep(3)
return item * 2
items = [1, 2, 3, 4]
async for result in aioeachlimit(items, f, concurrency_limit=2):
print(result) # Prints 2 4 6 8 in random order
如果您不需要返回任何内容:
await aioeachlimit(items, f, concurrency_limit=2, discard_results=True)
如果items
是asyncio.Queue
,那么aioEachLimit将无限期地从中读取。
测试
pytest .