python - 无法摆脱异步循环
问题描述
我正在尝试创建一个命令,该命令在变量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())
解决方案
您需要返回一些内容来让循环知道它已经完成,而不是尝试在异步函数上使用 while 循环。( loop.run_until_complete(main())
),你希望循环结束的地方,把return
.
推荐阅读
- javascript - 复制 dataLayer 正在返回 {object Object}
- substrate - 基板中的事件存储成本是多少?
- sed - 如何附加文件 n 次并使用 sed 对块进行编号?
- r - 在数据框中创建一个作为总和、按类别或不同变量的新列。(我曾经在 STATA 中使用 bysort egen total 执行此操作)
- postgresql - 在 postgresql 的函数中的输入参数中转义单引号
- r - 将三个数据框的内容合并到 R 中的一列(跨多列)
- ansible - 如何使用`json_query`过滤和重命名hasmap列表中的元素
- javascript - PDFJS Chrome 扩展
- c# - 验证设备的自签名证书
- javascript - 如何在 ES6 中重新导出修改后的导入函数列表