如何知道反应堆的所有过程都已完成

时间:2012-12-03 19:23:23

标签: python twisted

我是twisted和python的新手,我正在阅读扭曲的python。

注册电话

reactor.callLater(_interval, self.count, *args)

我创建了Countdown类,它包含count函数,我正在调用它三次

reactor.callWhenRunning(Countdown().count, 1)
reactor.callWhenRunning(Countdown().count, 2)
reactor.callWhenRunning(Countdown().count, 3)

时间间隔不同,我需要在所有呼叫完成后停止反应堆。所以在twisted API中有一些方法可以知道所有的调用都已完成。

我的代码是

class Countdown(object):

    counter = 5

    def count(self, *args):
    _interval = args[0]
        name = args[1]
        if self.counter == 0 and name == "Third Call":
            reactor.stop()
        elif self.counter == 0:
        print name, " Finished..!"
        else:
            print self.counter, '...'
            self.counter -= 1
            reactor.callLater(_interval, self.count, *args)

from twisted.internet import reactor

reactor.callWhenRunning(Countdown().count, 1, "First Call")
reactor.callWhenRunning(Countdown().count, 2, "Second Call")
reactor.callWhenRunning(Countdown().count, 3, "Third Call")

print 'Start!'
reactor.run()
print 'Stop!'

现在我正在使用     如果self.counter == 0且name ==“Third Call”: 防止我的所有反制过程完成。所以现在我需要知道,是否有扭曲的内置方法知道所有呼叫已完成或我的所有呼叫完成。

3 个答案:

答案 0 :(得分:1)

Twisted有一个用于处理事件列表的API:DeferredList http://twistedmatrix.com/documents/current/core/howto/defer.html#auto8

以下是一个小例子:

from twisted.internet import defer 
from twisted.internet import reactor


def delayedFunction(dF):
    print('I was called')
    dF.success(True)

class Counter(object):
    def timeOffsetExecute(self,delay):
        dF = defer.Deferred()
        reactor.callLater(delay,delayedFunction,dF)
        return dF

def onAllResult(val):
    print 'All delayed functions called'
    reactor.stop()

cp = Counter()

dl = defer.DeferredList([cp.timeOffsetExecute(1), cp.timeOffsetExecute(3), cp.timeOffsetExecute(9)], consumeErrors=True)
dl.addCallback(onAllResult)

reactor.run()

答案 1 :(得分:1)

我正在从Twisted Introduction进行相同的练习,并发现解决方案非常简单(假设它应该在不使用延迟的情况下解决),并使用共享静态变量来计算正在运行的实例:

from twisted.internet import reactor

class Countdown(object):

    running = 0

    def __init__(self, value, delay=1):
        self.delay = delay
        self.counter = value
        Countdown.running += 1

    def __call__(self):
        if self.counter == 0:
            Countdown.running -= 1
            if Countdown.running == 0:
                reactor.stop()
        else:
            print self.counter, '...'
            self.counter -= 1
            reactor.callLater(self.delay, self)

reactor.callWhenRunning(Countdown(10, 0.5))
reactor.callWhenRunning(Countdown(5, 2))
reactor.callWhenRunning(Countdown(7, 1.5))

print 'Start!'
reactor.run()
print 'Stop!' 

答案 2 :(得分:0)

我的看法:

class Countdown(object):

counter1 = 5
counter2 = 20
counter3 = 50

def count1(self):
    if self.counter1 == 0 and self.counter2 == 0 and self.counter3 == 0:
        reactor.stop()
    elif self.counter1 > 0:
        print self.counter1, '... process',1
        self.counter1 -= 1
        reactor.callLater(0.1, self.count1)

def count2(self):
    if self.counter1 == 0 and self.counter2 == 0 and self.counter3 == 0:
        reactor.stop()
    elif self.counter2 > 0:
        print self.counter2, '... process',2
        self.counter2 -= 1.25
        reactor.callLater(0.12, self.count2)

def count3(self):
    if self.counter1 == 0 and self.counter2 == 0 and self.counter3 == 0:
        reactor.stop()
    elif self.counter3 > 0:
        print self.counter3, '... process',3
        self.counter3 -= 12.5
        reactor.callLater(0.2345, self.count3)

from twisted.internet import reactor

obj = Countdown()
reactor.callWhenRunning(obj.count1)
reactor.callWhenRunning(obj.count2)
reactor.callWhenRunning(obj.count3)

print 'Start!'
reactor.run()
print 'Stop!'

这里我没有使用任何延迟方法解决方案。