简体   繁体   English

从未等待过 python asyncio 协程

[英]python asyncio coroutine was never awaited

I am using python 3.8 with this code我在此代码中使用 python 3.8

async def main():
    pass

async def build():
    pass

asyncio.create_task(build())
loop = asyncio.get_event_loop()
asyncio.create_task(main())
pending = asyncio.all_tasks()
loop.run_until_complete(asyncio.gather(*pending))

and get the following error并得到以下错误

sys:1: RuntimeWarning: coroutine 'build' was never awaited sys:1: RuntimeWarning: coroutine 'build' 从未等待

What am I missing here?我在这里缺少什么? shouldn't run until complete wait for all the tasks to finish?不应该运行直到完成等待所有任务完成?

As the loop is not running when creating the task, asyncio is unable to attach tasks to it.由于创建任务时循环未运行,因此asyncio无法将任务附加到它。

This can be fixed by replacing asyncio.create_task() with loop.create_task() .这可以通过用asyncio.create_task()替换asyncio.create_task()loop.create_task()

The full error can be retrieved using asyncio.gather(..., return_exceptions=True) so gather() will raise a RuntimeError: no running event loop .可以使用asyncio.gather(..., return_exceptions=True)检索完整错误asyncio.gather(..., return_exceptions=True)因此gather()将引发RuntimeError: no running event loop

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

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