简体   繁体   English

RuntimeWarning:从未等待协程“ main”

[英]RuntimeWarning: coroutine 'main' was never awaited

I'm trying to parse and put JSON async but getting: 我正在尝试解析并将JSON异步处理,但得到:

RuntimeWarning: coroutine 'main' was never awaited RuntimeWarning:从未等待协程“ main”

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()

How I could use async correctly? 如何正确使用异步?

As stated in the error message, you have to await for your main function as it is asynchronous. 如错误消息中所述,您必须等待主要功能,因为它是异步的。 Borrowed from the Python3.7 documentation section about coroutines Python3.7文档部分借来的有关协程的信息

Note that simply calling a coroutine will not schedule it to be executed 请注意,仅调用协程将不会安排其执行

Given the fact that you want to run your toplevel entrypoint, in Python 3.7+ you should use 考虑到您要运行顶级入口点的事实,在Python 3.7+中应该使用

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

For earlier versions you have to handle the event loop yourself: 对于早期版本,您必须自己处理事件循环:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM