gpt4 book ai didi

python - 运行时警告 : coroutine 'main' was never awaited

转载 作者:行者123 更新时间:2023-12-01 10:19:00 24 4
gpt4 key购买 nike

我正在尝试解析和放置 JSON 异步但得到:

RuntimeWarning: coroutine 'main' was never awaited


import asyncio
import aiohttp

async def get_put_content(url_get, url_put, session):
async with session.get(url_get) as response:
data = await response.read()

async with session.put(url_put, data=data) as response:
print(response.status)

async def main():
async with aiohttp.ClientSession() as session:
for temp_id in range (1, 100):
api_url = "https://api.link" + str(temp_id)
bd_url = "http://127.0.0.1:5984/photosget/" + str(temp_id)
asyncio.create_task(get_put_content(api_url, bd_url, session))

asyncio.wait(get_put_content)

if __name__ == '__main__':
main()

我如何正确使用异步?

最佳答案

如错误消息中所述,您必须等待主函数,因为它是异步的。借用the Python3.7 documentation section about coroutines

Note that simply calling a coroutine will not schedule it to be executed



鉴于您想运行顶级入口点,在 Python 3.7+ 中,您应该使用

if __name__ == "__main__":
asyncio.run(main())

对于早期版本,您必须自己处理事件循环:

if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())

关于python - 运行时警告 : coroutine 'main' was never awaited,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57399157/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com