2012-12-03 95 views
1

我是新來扭曲和蟒蛇,我正在閱讀扭曲的蟒蛇。如何知道反應器的所有過程都已完成

登記呼叫

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

我已經創建了倒計時類,它包含計數功能,我稱這三個時間

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

現在我使用 如果self.counter == 0和名稱==「第三個電話」: 防止我所有的反過程結束。所以現在我需要知道,是否有扭曲有任何內置的方法知道所有通話已完成或我的所有計數器通話完成。

+0

這有點令人困惑。你使用'callLater'還是'callWhenRunning'?都?你想知道所有四個電話何時發生?或者當發生特別事件時? –

+0

對不起我的英語不好。我已經更新了問題描述中的代碼,請讓您查看它併爲我提供最佳方式。 – Ayaz

回答

1

扭曲具有與事件列表工作的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() 
+0

介意你 - 我沒有蟒蛇或扭曲在我發佈的機器上,所以這可能會被破壞。但是這個想法如上所述。 – jbreicis

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

在這裏,我沒有使用任何遞延法解決。

相關問題