我有以下方法为我生成数据:
async def fetch_url(self, video_id):
data = await self.s3.generate_presigned_url(...video_id...)
return data
def convert_to_json(self, data):
loop = asyncio.get_event_loop()
tasks = []
urls = [row[0] for row in data]
for url in urls:
tasks.append(fetch_url(url))
loop.run_until_complete(asyncio.gather(*tasks))
loop.close()
如何将 fetch_url
的结果存储在某个列表中?
最佳答案
… If all the tasks are done successfully, the returned future’s result is the list of results (in the order of the original sequence, not necessarily the order of results arrival). …
也就是说,如果您等待
收集的结果,您将获得已获取的数据
的列表
Return the Future’s result, or raise its exception.
即run_until_complete
会返回结果gather
,即获取到的数据
列表。
存储的结果很简单:
...
all_data = loop.run_until_complete(asyncio.gather(*tasks))
loop.close()
return all_data
关于python - 如何在异步方法中将结果存储在列表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43226402/