我正在寻找将以下代码转换为并行执行的方法:
urls = ['http://example1.org', 'http://example2.org', '...']
def getResults(urls):
results = {}
for url in urls:
results[url] = getResult(url)
return results
def getResult(url):
return get(url).json()
这是我尝试过的:
urls = ['http://example1.org', 'http://example2.org', '...']
def getResults(urls):
return asyncio.gather((getResult(url)) for url in urls)
async def getResult(url):
return await get(url).json()
我在正确的轨道上吗?在python 3中使用async / await的正确方法是什么?
谢谢