Python 尝试调用self时出现扭曲错误
当我运行我的听众时,我得到了一个奇怪的断言错误。本课程中逻辑的Python 尝试调用self时出现扭曲错误,python,twisted,Python,Twisted,当我运行我的听众时,我得到了一个奇怪的断言错误。本课程中逻辑的if部分有效,我从本教程中获取了扭曲的代码: 错误 --- <exception caught here> --- File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 429, in _continueFiring callable(*args, **kwargs) File "sponzyTwisted.p
if
部分有效,我从本教程中获取了扭曲的代码:
错误
--- <exception caught here> ---
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 429, in _continueFiring
callable(*args, **kwargs)
File "sponzyTwisted.py", line 17, in count
reactor.callLater(1, self.counter)
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 705, in callLater
assert callable(_f), "%s is not callable" % _f
exceptions.AssertionError: 19 is not callable
-----
文件“/usr/lib/python2.7/dist-packages/twisted/internet/base.py”,第429行,在continueFiring中
可调用(*args,**kwargs)
文件“sponzyTwisted.py”,第17行,计数
反应器呼叫器(1,自计数器)
文件“/usr/lib/python2.7/dist-packages/twisted/internet/base.py”,第705行,稍后调用
断言可调用(\u f),%s不可调用“%\u f
exceptions.AssertionError:19不可调用
您应该向callLater
提供一个可调用对象,而您提供的是简单的int计数器。您应该将实际方法count
作为可调用方法传递,如下所示:
class controlListener(object):
counter = 20
def count(self):
if self.counter == 0:
print "Killing Process"
reactor.stop()
else:
print self.counter, '...'
self.counter -= 1
reactor.callLater(1, self.count)
这太棒了,它可以工作,但controlListener没有将优先级传递给printStuff反应堆。我使用了错误的回调吗?我想让control listenr执行一个交互式操作,然后传递给其他对象,我应该使用哪个调用?回调对我来说似乎是正确的,但我对Twisted没有经验。运行时只需调用reactor.callwhen(controlListener().count)
class controlListener(object):
counter = 20
def count(self):
if self.counter == 0:
print "Killing Process"
reactor.stop()
else:
print self.counter, '...'
self.counter -= 1
reactor.callLater(1, self.count)