小编Ale*_*ger的帖子

asyncio 在结果进来时收集产生的结果

我希望能够从一组运行的任务中产生结果,gather因为它们进入进一步处理。

# Not real code, but an example

async for response in asyncio.gather(*[aiohttp.get(url) for url in ['https://www.google.com', 'https://www.amazon.com']]):
    await process_response(response)
Run Code Online (Sandbox Code Playgroud)

目前,我可以使用该gather方法全部get并发运行,但必须等到它们全部完成才能处理它们。我还是 Python async/await 的新手,所以也许我缺少一些明显的方法来做到这一点。

# What I can do now
responses = await asyncio.gather(*[aiohttp.get(url) for url in ['https://www.google.com', 'https://www.amazon.com']])
await asyncio.gather(*[process_response(response) for response in responses])

Run Code Online (Sandbox Code Playgroud)

谢谢!

python python-asyncio

3
推荐指数
1
解决办法
617
查看次数

标签 统计

python ×1

python-asyncio ×1