Python 如何知道反应器所有过程都已完成

Python 如何知道反应器所有过程都已完成,python,twisted,Python,Twisted,我不熟悉twisted和python,我正在阅读twisted python 挂号电话 reactor.callLater(_interval, self.count, *args) 我创建了倒计时类,它包含计数函数,我调用了三次 reactor.callWhenRunning(Countdown().count, 1) reactor.callWhenRunning(Countdown().count, 2) reactor.callWhenRunning(Countdown().count

我不熟悉twisted和python,我正在阅读twisted python

挂号电话

reactor.callLater(_interval, self.count, *args)
我创建了倒计时类,它包含计数函数,我调用了三次

reactor.callWhenRunning(Countdown().count, 1)
reactor.callWhenRunning(Countdown().count, 2)
reactor.callWhenRunning(Countdown().count, 3)
不同的时间间隔,我需要在所有调用完成后停止反应器。所以在TwistedAPI中,有一些方法可以知道所有调用都已完成

我的代码是

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==“第三次调用”:
为了防止我的所有计数器进程都已完成。所以现在我需要知道,twisted中是否有任何内置方法可以知道所有调用都已完成或我的所有计数器调用已完成。

twisted有一个用于处理事件列表的API:DeferredList

下面是一个小示例,说明了这可能的工作原理:

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()

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

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!' 
我的看法:

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!'

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

这有点令人困惑。您是在运行时使用
callLater
还是
call
?二者都你想知道这四个电话是什么时候打完的吗?或者什么时候发生了这样的事情?对不起,我的英语不好。我已经更新了关于问题描述的代码,请让你检查一下,并建议我最好的方法。请注意,我在我发布的机器上没有python或Twist,所以这可能会被破坏。但想法如上所述。