为什么会这样异步.iscoroutine在生成器上返回True?

2024-04-18 07:47:39 发布

您现在位置:Python中文网/ 问答频道 /正文

请考虑以下功能:

def test(): yield 1

asyncio.iscoroutine()方法将返回方法test()True。另一件需要注意的事情是inspect.iscoroutine()将返回False。你知道吗

为什么asyncio认为这是一个协同程序?不管怎样,似乎其中一个弄错了。你知道吗


Tags: 方法test功能asynciofalsetruedef事情
1条回答
网友
1楼 · 发布于 2024-04-18 07:47:39

这看起来像是新的python3.7asyncio文档中的一个文档错误。在提出问题和回答这个问题时,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从异步.iscoroutine文件。我会贴一张存档.org显示旧文本的链接,但是存档.org没有存档该版本。不过,您仍然可以在github修订历史或我刚刚链接的提交中看到旧文本。你知道吗

相关问题 更多 >