1

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

挂号电话

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

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

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

具有不同的时间间隔,我需要在所有呼叫完成后停止反应器。因此,在扭曲的 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!'

现在我使用 if self.counter == 0 和 name == "Third Call": 来防止我的所有计数器进程完成。所以现在我需要知道,twisted 中是否有任何内置方法可以知道所有呼叫已完成或我的所有计数器呼叫已完成。

4

3 回答 3

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()
于 2012-12-05T14:36:31.253 回答
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!' 
于 2015-02-17T07:47:55.647 回答
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!'

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

于 2016-12-28T17:23:35.003 回答