首页 > 解决方案 > 如何检查 Python 中没有等待就调用的异步函数?

问题描述

假设我们有以下功能:

async def x1():
    print("inside x1")

async def x2():
    print("inside x2")

async def main():
    await x1()
    x2()
    await asyncio.sleep(5) 

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

我将收到以下警告:

RuntimeWarning: coroutine 'x2' was never awaited

有什么方法可以让 Python 引发错误而不仅仅是打印警告?

标签: asynchronouspython-asyncio

解决方案


推荐阅读