如何使用async for循环迭代列表?

时间:2018-01-01 18:32:59

标签: python python-3.x asynchronous iterator python-asyncio

所以我需要为列表中的所有项目调用async函数。这可以是一个URL列表和一个使用aiohttp的异步函数,它从每个URL获取响应。现在显然我不能做到以下几点:

async for url in ['www.google.com', 'www.youtube.com', 'www.aol.com']:

我可以使用普通的for循环但是我的代码会同步运行,我失去了获得async响应提取功能的好处和速度。

有什么方法可以转换列表,以便上述工作吗?我只需要将列表的__iter__()更改为__aiter__()方法吗?这可以通过子类化列表来实现吗?也许将它封装在一个类中?

2 个答案:

答案 0 :(得分:11)

使用asyncio.as_completed

yPos

asyncio.gather

for future in asyncio.as_completed(map(fetch, urls)):
    result = await future

编辑:如果您不介意拥有外部依赖关系,可以使用aiostream.stream.map

results = await asyncio.gather(map(fetch, urls))

您可以使用from aiostream import stream, pipe async def fetch_many(urls): xs = stream.iterate(urls) | pipe.map(fetch, ordered=True, task_limit=10) async for result in xs: print(result) 参数控制同时运行的fetch coroutine的数量,并选择是按顺序还是尽快获得结果。

请参阅此demonstrationdocumentation中的更多示例。

答案 1 :(得分:2)

请注意,文森特的回答有部分问题:
您必须在map函数的前面有一个splatter operator,否则asyncio.gather会尝试使用整个列表。所以这样做:

results = await asyncio.gather(*map(fetch, url))