twisted循环调用访问类变量
我有一个这样的例子
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,我会得到
exceptions.AttributeError: Intake instance has no attribute 'counters'
1 个回答
3
如果你能提供一个可以运行的例子来说明你的问题,那会是个好主意。因为我试了你描述的内容,结果是没问题的。
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()