错误:types.coroutine() 需要一个可调用的 [英] Error: types.coroutine() expects a callable

查看:28
本文介绍了错误:types.coroutine() 需要一个可调用的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程:

from tornado import gen类 VertexSync(顶点):@wait_til_complete@gen.coroutine@类方法def find_by_value(cls, *args, **kwargs):流 = 产量超级().find_by_value(*args,**kwargs)聚合 = []为真:resp = yield stream.read()如果响应为无:休息聚合 = 聚合 + 响应返回聚合

<块引用>

TypeError: types.coroutine() 需要一个可调用的

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

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

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

解决方案

问题在于 classmethod 做了...有趣的事情.一旦类定义完成,你就有了一个很好的可调用方法,但是在定义过程中你有一个classmethod对象,它不是't 可调用:

<预><代码>>>>a = classmethod(lambda self: None)>>>一种<0x10b46b390处的类方法对象>>>>可调用(一)错误的>>>一种()回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:classmethod"对象不可调用

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

@gen.coroutine@类方法定义事物(...):...

您正在尝试将协程转换为类方法:

@classmethod@gen.coroutine定义事物(...):...

请注意,装饰器被应用由内而外",参见例如装饰器执行顺序

I have the following 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

Can you tell me what the problem is?

=> edit Code calling this function

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

解决方案

The problem is that classmethod does... interesting things. Once the class definition has finished you have a nice callable method on the class, but during the definition you have a classmethod object, which isn't callable:

>>> 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

The simplest fix is to re-order the decorators, so that rather than trying to turn a classmethod into a coroutine:

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

you are trying to turn a coroutine into a classmethod:

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

Note that the decorators are applied "inside out", see e.g. Decorator execution order

这篇关于错误:types.coroutine() 需要一个可调用的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆