【问题标题】:twisted loopingcall access class vars扭曲循环调用访问类变量
【发布时间】:2012-01-06 20:23:32
【问题描述】:

我有下面的例子

class Intake:
    def __init__(self):

        #
        # aggregate dict to store all the counters
        #
        self.counters = {}

        #
        # start a looping call to run reach minute
        #
        self.lc = task.LoopingCall(self.aggregate, self.counters)
        self.lc.start(60)


    def aggregate(self, counters):
        print counters

所以效果很好.. 但在我的聚合函数中,我需要清除 self.counters 字典。我在执行此操作时遇到问题..

我想做类似的事情

    def aggregate(self, counters):
        print counters

        self.counters = {}

如果我在该函数中引用 self.counters,我会得到 ​​p>

exceptions.AttributeError: Intake instance has no attribute 'counters'

【问题讨论】:

  • ...那么问题是什么?那不行吗?
  • 我编辑了问题以在底部添加错误

标签: python twisted


【解决方案1】:

最好包含一个可运行的问题示例,如果我尝试您所描述的,它会正常工作。

from twisted.internet import task

class Intake:
    def __init__(self):

        #
        # aggregate dict to store all the counters
        #
        self.counters = {}
        self.count = 0
        #
        # start a looping call to run reach minute
        #
        self.lc = task.LoopingCall(self.aggregate, self.counters)
        self.lc.start(1)


    def aggregate(self, counters):
        print '%d, %r, %r' % (self.count, counters, self.counters)
        self.count += 1
        self.counters = {}

if __name__ == "__main__":
    from twisted.internet import reactor
    r = Intake()
    reactor.run()

【讨论】:

    猜你喜欢
    • 2012-10-16
    • 1970-01-01
    • 2020-10-19
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多