反应堆停止的时间比我预期的要早?
感谢本教程和许多其他教程,我正在尝试自学一些基本的 Twisted 编程。我已经看到这个当前的例子,我无法弄清楚它为什么要做它正在做的事情。
简短摘要:我已经实例化了三个反应器,它们从 5 倒数到 1,并具有不同的计数延迟。唯一的问题是,看起来当第一个计数器(延迟最短)达到 0 时,它不仅停止自己的反应堆,还停止所有其他反应堆。
#!/usr/bin/env python
class Countdown(object):
counter = 5
def count1(self):
from twisted.internet import reactor
if self.counter == 0:
reactor.stop()
else:
print self.counter, '...1'
self.counter -= 1
reactor.callLater(1, self.count1)
def count2(self):
from twisted.internet import reactor
if self.counter == 0:
reactor.stop()
else:
print self.counter, '...2'
self.counter -= 1
reactor.callLater(0.5, self.count2)
def count3(self):
from twisted.internet import reactor
if self.counter == 0:
reactor.stop()
else:
print self.counter, '...3'
self.counter -= 1
reactor.callLater(0.25, self.count3)
from twisted.internet import reactor
reactor.callWhenRunning(Countdown().count1)
reactor.callWhenRunning(Countdown().count2)
reactor.callWhenRunning(Countdown().count3)
print 'Start!'
reactor.run()
print 'Stop!'
输出
Start!
5 ...1
5 ...2
5 ...3
4 ...3
4 ...2
3 ...3
2 ...3
4 ...1
3 ...2
1 ...3
Stop!
我的印象是,虽然所有三个计数器都应该以自己的速度倒数并完成其 5->0 进程,但程序会等待它们全部完成后再退出。我是否误解了 Twisted 的某些方式?
I am trying to teach myself some rudimentary Twisted programming thanks to this tutorial and many others. I have come to this current example which I Can't figure out why it's doing what it is doing.
Short summary: I have instantiated three reactors that count down from 5 to 1 with different delays in their counting. Only thing is, it looks like when the first counter (with the shortest delay) gets to 0, it stops not only its own reactor, but all the others.
#!/usr/bin/env python
class Countdown(object):
counter = 5
def count1(self):
from twisted.internet import reactor
if self.counter == 0:
reactor.stop()
else:
print self.counter, '...1'
self.counter -= 1
reactor.callLater(1, self.count1)
def count2(self):
from twisted.internet import reactor
if self.counter == 0:
reactor.stop()
else:
print self.counter, '...2'
self.counter -= 1
reactor.callLater(0.5, self.count2)
def count3(self):
from twisted.internet import reactor
if self.counter == 0:
reactor.stop()
else:
print self.counter, '...3'
self.counter -= 1
reactor.callLater(0.25, self.count3)
from twisted.internet import reactor
reactor.callWhenRunning(Countdown().count1)
reactor.callWhenRunning(Countdown().count2)
reactor.callWhenRunning(Countdown().count3)
print 'Start!'
reactor.run()
print 'Stop!'
Output
Start!
5 ...1
5 ...2
5 ...3
4 ...3
4 ...2
3 ...3
2 ...3
4 ...1
3 ...2
1 ...3
Stop!
I was under the impression that while all three counters should count down at their own speed and complete their 5->0 progression, the program would wait for them all to complete before exiting. Am I misunderstanding something in the ways of Twisted here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不熟悉扭曲,但从浏览谷歌结果来看,反应堆似乎是一个事件循环。您只有其中一个,因此第一个命中reactor.stop()的计数器会停止循环。
要执行您想要的操作,您需要删除reactor.stop()调用并构造事物,以便当最后一个计时器到达末尾时,只有它调用reactor.stop()
I am unfamiliar with twisted, but from skimming google results it looks like reactor is an event loop. You only have one of them, so the first counter to hit reactor.stop() stops the loop.
To do what you want you need to remove the reactor.stop() calls and structure things so that when the last timer hits the end it, and only it, calls reactor.stop()
他在第 3 部分中对此进行了一些讨论。您可能会混淆“在一台服务器中启动三个反应器”和“启动三个不同的服务器”。这些例子建议采取后者;您的代码正在尝试执行前者。
基本上,反应器是一个单例,一旦停止就无法重新启动。所以每个进程只能有一个,或者每个线程只能有一个。
您不想启动三个反应器,而是希望在同一个反应器上设置三个不同的定时回调。他们都会在适当的时候被解雇。
He talks a bit about that in part 3. You might be mixing up "starting three reactors in one server" and "starting three different servers". The examples suggest to do the latter; your code is attempting to do the former.
Basically the reactor is a singleton, and cannot be restarted once stopped. So you can only have one per process, or maybe one per thread.
Instead of starting three reactors, you want to set up three different timed callbacks on the same reactor. They will all get fired at the appropriate time.