Python 中的异步是什么?

What is async in Python?

我读到了新的 Python "keywords" asyncawait。然而,它们既不是真正的关键字,也不是命名空间中的保留内容。

>>> import keyword
>>> keyword.iskeyword("async")
False
>>> async
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'async' is not defined

在示例中,我希望关键字为 TrueSyntaxError

那么,Python中的async到底是什么?它是如何工作的?

为了向后兼容,在 Python 3.5 和 3.6 中,asyncawait 通过 ugly tokenizer hack 解析。在 async def 函数定义中,或者对于 def 之前的 async,分词器将 asyncawaitNAME 标记替换为 ASYNCAWAIT 代币;在其他情况下,标记器为 asyncawait 发出常规 NAME 标记,将它们视为标识符。

您可以在 Parser/tokenizer.c, and you can find the backward compatibility plan in PEP 492 中看到处理它的代码,引入 asyncawait 语法的 PEP。