Python 确保aiohttp/asyncio中递归函数的未来

Python 确保aiohttp/asyncio中递归函数的未来,python,python-asyncio,Python,Python Asyncio,我遇到了一个问题,在事件循环中没有正确调用递归函数。我有以下资料: async def get_response(session, url, attempt=0): async with session.get(url) as response: if response.status == 200: return await response.json() elif attempt < 3: # Thi

我遇到了一个问题,在事件循环中没有正确调用递归函数。我有以下资料:

async def get_response(session, url, attempt=0):
    async with session.get(url) as response:
        if response.status == 200:
            return await response.json()
        elif attempt < 3:
            # This never gets called, a coroutine is returned
            return get_response(session, url, attempt + 1)
        else:
            return None

async def main(loop):
    output = []
    urls = ["https://httpbin.org/json"]
    async with aiohttp.ClientSession(loop=loop) as session:
        tasks = []
        for url in urls:
            tasks.append(asyncio.ensure_future(get_response(session, url)))
        for future in await asyncio.gather(*tasks):
            output.append(future)
    return output

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    output = loop.run_until_complete(main(loop))
async def get_响应(会话、url、尝试=0):
以session.get(url)作为响应的异步:
如果response.status==200:
return wait response.json()
elif尝试<3:
#这不会被调用,会返回一个协程
返回get_响应(会话、url、尝试+1)
其他:
一无所获
异步def主(循环):
输出=[]
URL=[”https://httpbin.org/json"]
将aiohttp.ClientSession(loop=loop)作为会话异步:
任务=[]
对于url中的url:
tasks.append(asyncio.sure_future(获取_响应(会话,url)))
对于等待asyncio.gather(*任务)中的未来:
output.append(未来)
返回输出
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
loop=asyncio.get\u event\u loop()
输出=循环。运行直到完成(主(循环))
问题出现在
get\u response
中,如果
尝试<3
它实际上不会再次运行该函数上述代码可以正常工作,因为
https://httpbin.org/json
将返回一个200代码。
如果我错了,请纠正我(因为我可能错了),但我认为由于该函数的迭代不在
main
中的任务列表中,因此它实际上不会运行?当我调试时,我得到的回报是一个实际的协同程序对象,而不是我所期望的
None


如果需要,什么是确保
未来
中的递归函数将被再次调用的正确方法?

由于
get\u response
是异步的,您需要在调用它时等待它,即使是从它自身进行调用。因此,您需要更改:

返回get\u响应(会话、url、尝试+1)
致:

return wait get_响应(会话、url、尝试+1)