python - 为什么 asyncio.iscoroutine 在生成器上返回 True?

标签 python python-3.x python-asyncio

请考虑以下函数:

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/

相关文章:

python - 从西类牙语解析日期和更改格式

python - 文件 “test_hdfs.py”,save_path = saver.save(sess,hdfs_path +“save_net.ckpt”) “Parent directory of {} doesn'存在,无法保存。”。format(save_path))

python - 多个Python类继承

python - 与 Flask 并行运行 URL 请求

python - 从未检索到 future 的异常

python - asyncio.CancelledError 未被捕获

python - _mysql_exceptions.OperationalError : 1045,“用户访问被拒绝

python - networkx 是否具有计算权重的路径长度的功能?

python - 当我的计算机上有 Python 2、Python 3 和 Anaconda 时,如何控制将包 pip 安装到哪个 Python 发行版?

json - Safaricom 达拉贾 : Duplicate json key detected