【发布时间】:2016-08-09 12:38:51
【问题描述】:
我的扭曲程序可以运行,但现在我的一个反应堆没有将优先级传递给其他反应堆。我希望 controlListener 反应器进行一次迭代,然后将优先级传递给 printstuffs 反应器。
#Random class as proof of concept
class printStuffs(object):
print "counting "
printerCount = 0
def count(self):
self.printerCount = self.printerCount + 1
print ("the counter is at " + str(self.printerCount))
##########################################################################
## The control listneer class is designed to kill given reactor threads ##
## on demand from something once it recieves a signal it is supposed ##
## to do one ieteration then release ##
##########################################################################
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)
from twisted.internet import reactor
print "Printing random stuff"
reactor.callWhenRunning(printStuffs().count)
print "Intializing kill listner"
reactor.callWhenRunning(controlListener().count)
reactor.run()
print "Process killed"
这是输出
Printing random stuff
Intializing kill listner
the counter is at 1
20 ...
19 ...
18 ...
17 ...
16 ...
15 ...
14 ...
13 ...
12 ...
11 ...
10 ...
9 ...
8 ...
7 ...
6 ...
5 ...
4 ...
3 ...
2 ...
1 ...
Killing Process
Process killed
我想让它做类似的事情
the counter is at 1
20 ...
the counter is at 2
the counter is at 3
19 ...
等等。
有什么想法吗?
【问题讨论】:
-
我从来没有使用过twisted,但会不会是
printStuffs.count()根本不像reactor.callLater()那样使用controlListener.count()来重新调度自己? -
你知道了,如果你想要信用将你的帖子复制成一个实际的答案,我会奖励它。
-
您已经收到了答复,但我只想注意,twisted (twistedmatrix.com/documents/current/api/…) 中有一个 LoopingCall,它可以让您不必手动重新安排重复的呼叫。