Python 中的异步是什么?
What is async in Python?
我读到了新的 Python "keywords" async
和 await
。然而,它们既不是真正的关键字,也不是命名空间中的保留内容。
>>> import keyword
>>> keyword.iskeyword("async")
False
>>> async
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'async' is not defined
在示例中,我希望关键字为 True
和 SyntaxError
。
那么,Python中的async
到底是什么?它是如何工作的?
为了向后兼容,在 Python 3.5 和 3.6 中,async
和 await
通过 ugly tokenizer hack 解析。在 async def
函数定义中,或者对于 def
之前的 async
,分词器将 async
和 await
的 NAME
标记替换为 ASYNC
和 AWAIT
代币;在其他情况下,标记器为 async
和 await
发出常规 NAME
标记,将它们视为标识符。
您可以在 Parser/tokenizer.c
, and you can find the backward compatibility plan in PEP 492 中看到处理它的代码,引入 async
和 await
语法的 PEP。
我读到了新的 Python "keywords" async
和 await
。然而,它们既不是真正的关键字,也不是命名空间中的保留内容。
>>> import keyword
>>> keyword.iskeyword("async")
False
>>> async
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'async' is not defined
在示例中,我希望关键字为 True
和 SyntaxError
。
那么,Python中的async
到底是什么?它是如何工作的?
为了向后兼容,在 Python 3.5 和 3.6 中,async
和 await
通过 ugly tokenizer hack 解析。在 async def
函数定义中,或者对于 def
之前的 async
,分词器将 async
和 await
的 NAME
标记替换为 ASYNC
和 AWAIT
代币;在其他情况下,标记器为 async
和 await
发出常规 NAME
标记,将它们视为标识符。
您可以在 Parser/tokenizer.c
, and you can find the backward compatibility plan in PEP 492 中看到处理它的代码,引入 async
和 await
语法的 PEP。