【发布时间】:2015-01-13 05:26:17
【问题描述】:
我正在学习 Python,目前专注于 Twisted...我正在做一个教程练习,但我似乎无法理解为什么第二版代码中的修改会导致“计数”功能在反应堆启动之前执行。所改变的只是将 's' 参数添加到函数中。
工作:
class Countdown(object):
counter = 5
def count(self):
if self.counter == 0:
reactor.stop()
else:
print self.counter, '...'
self.counter -= 1
reactor.callLater(1, self.count)
from twisted.internet import reactor
reactor.callWhenRunning(Countdown().count)
print 'Start!'
reactor.run()
print 'Stop!'
破碎:
class Countdown(object):
counter = 5
def count(self,s):
if self.counter == 0:
reactor.stop()
else:
print self.counter, '...'
self.counter -= 1
reactor.callLater(1, self.count(1))
from twisted.internet import reactor
reactor.callWhenRunning(Countdown().count(1))
print 'Start!'
reactor.run()
print 'Stop!'
这是回溯:
Traceback (most recent call last):
File "C:\Python27\twistedexample\basic-twisted\countdown.py", line 15, in <module>
reactor.callWhenRunning(Countdown().count(1))
File "C:\Python27\twistedexample\basic-twisted\countdown.py", line 11, in count
reactor.callLater(1, self.count(1))
File "C:\Python27\twistedexample\basic-twisted\countdown.py", line 11, in count
reactor.callLater(1, self.count(1))
File "C:\Python27\twistedexample\basic-twisted\countdown.py", line 11, in count
reactor.callLater(1, self.count(1))
File "C:\Python27\twistedexample\basic-twisted\countdown.py", line 11, in count
reactor.callLater(1, self.count(1))
File "C:\Python27\twistedexample\basic-twisted\countdown.py", line 11, in count
reactor.callLater(1, self.count(1))
File "C:\Python27\twistedexample\basic-twisted\countdown.py", line 7, in count
reactor.stop()
File "C:\Python27\lib\site-packages\twisted\internet\base.py", line 580, in stop
"Can't stop reactor that isn't running.")
ReactorNotRunning: Can't stop reactor that isn't running.
对此的任何意见表示赞赏,我觉得我在这里遗漏了一些重要的东西,不想跳过它。
【问题讨论】:
-
你应该修复代码示例中的缩进,因为它是一个语法错误:)
-
哎呀!没注意。我很快就会在这里修复它。顺便说一句,仍然爱 Twisted
-
谢谢你,你这么说:)。