跳到内容

await-outside-async (PLE1142)

源自 Pylint 代码检查工具。

作用

检查在 async 函数之外使用 await 的情况。

为什么这不好?

async 函数之外使用 await 会导致语法错误。

示例

import asyncio


def foo():
    await asyncio.sleep(1)

建议改为

import asyncio


async def foo():
    await asyncio.sleep(1)

Notebook 行为

作为例外,允许在 Jupyter notebook 的顶层使用 await (参见:autoawait)。

参考