python - 错误 : types. coroutine() expects a callable

标签 python tornado

我有以下类(class):

from tornado import gen

class VertexSync(Vertex):
    @wait_till_complete
    @gen.coroutine
    @classmethod
    def find_by_value(cls, *args, **kwargs):
        stream = yield super().find_by_value(*args, **kwargs)
        aggr = []
        while True:
            resp = yield stream.read()
            if resp is None:
                break
            aggr = aggr + resp
        return aggr

TypeError: types.coroutine() expects a callable

你能告诉我问题是什么吗?

=> 编辑 调用此函数的代码

print(DemoVertex.find_by_value('longitude', 55.0))

最佳答案

问题是 classmethod 做了……有趣的事情。一旦类定义完成,您就在类上有了一个很好的可调用方法,但是在定义期间您有一个类方法对象,它不是可调用:

>>> a = classmethod(lambda self: None)
>>> a
<classmethod object at 0x10b46b390>
>>> callable(a)
False
>>> a()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'classmethod' object is not callable

最简单的解决方法是重新排序装饰器,而不是试图将类方法变成协程:

@gen.coroutine
@classmethod
def thing(...):
    ...

你正试图将一个协程变成一个类方法:

@classmethod
@gen.coroutine
def thing(...):
    ...

请注意,装饰器是“由内而外”应用的,参见例如Decorator execution order

关于python - 错误 : types. coroutine() expects a callable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37995862/

相关文章:

python - 如何使用 seaborn 生成高分辨率热图?

python - 分割字符串ValueError : need more than 1 value to unpack

Tornado 中页面上的 Python web 开发动态字段 id

linux - 当某些文件更改时如何使 Tornado 自动重启

python - Tornado 非阻塞 SMTP 客户端

python - 使用 BeautifulSoup 在 python 中提取链接标签之间的文本

python - 如何使用 dbus 为函数编写单元测试以从 Spotify 获取信息?

python - Tornado 或 Django 与 CGI 一起工作?

python - h5py:压缩管道中的复合数据类型和比例偏移

javascript - 使用 jQuery 和 Tornado 进行跨源资源共享 (CORS)