错误:types.coroutine()期望可调用

时间:2016-06-23 15:26:18

标签: python tornado

我有以下课程:

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()需要一个可调用的

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

=>编辑 代码调用此函数

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

1 个答案:

答案 0 :(得分:4)

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

>>> 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(...):
    ...

请注意,修饰符应用于&#34;内部&#34; ,请参阅例如Decorator execution order