首页 > 解决方案 > 无法摆脱异步循环

问题描述

我正在尝试创建一个命令,该命令在变量benbotLoop设置为 2之后开始,在beenbotLoop设置为 1 之后停止。但无论我做什么,它似乎都不会停止循环。这是我的代码:

idint = int(0)
benbotLoop = 1

displayName = input("What's the display name? ")
benbotLoop += 1

async def main():
    global idint
    async with aiohttp.ClientSession() as session:
            async with session.get("http://benbotfn.tk:8080/api/cosmetics/search/multiple", params={"displayName": displayName}) as r:
                data = await r.json()
                json_data = await r.json()
                list_type = json_data[idint]["type"]

                if list_type == "Outfit":
                    list_id = json_data[idint]["id"]
                    benbotLoop = 1
                    print(list_id)
                    print(benbotLoop)
                else:
                    idint += 1


while benbotLoop == 2:
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

标签: pythonwhile-loopasync-await

解决方案


您需要返回一些内容来让循环知道它已经完成,而不是尝试在异步函数上使用 while 循环。( loop.run_until_complete(main())),你希望循环结束的地方,把return.


推荐阅读