请考虑以下函数:
def test(): yield 1
asyncio.iscoroutine()
方法将为 test()
方法返回 True
。另一件需要注意的事情是 inspect.iscoroutine()
将返回 False
。
asyncio 认为它是协程的任何原因吗?无论如何,似乎他们中的一个人弄错了。
最佳答案
这看起来像是新的 Python 3.7 asyncio 文档中的文档错误。在提出问题和回答此问题时,new docs说
Return True if obj is a coroutine object.
This method is different from inspect.iscoroutine() because it returns True for generator-based coroutines decorated with @coroutine.
而old docs说
Return True if obj is a coroutine object, which may be based on a generator or an async def coroutine.
旧文档是正确的; asyncio.iscoroutine
将任何生成器对象视为协程,而不仅仅是由 @coroutine
修饰的生成器函数创建的对象。
按照提问者的issue report ,文档已 updated从 asyncio.iscoroutine 文档中删除“decorated with @coroutine”。我会发布一个 archive.org 链接,显示旧文本,但 archive.org 没有存档该版本。不过,您仍然可以在 github 修订历史记录或我刚刚链接的提交中看到旧文本。
关于python - 为什么 asyncio.iscoroutine 在生成器上返回 True?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52506066/