2011-10-28 44 views
2
from twisted.internet import reactor, defer 

def getDummyData(x): 
    """ 
    This function is a dummy which simulates a delayed result and 
    returns a Deferred which will fire with that result. Don't try too 
    hard to understand this. 
    """ 
    d = defer.Deferred() 
    # simulate a delayed result by asking the reactor to fire the 
    # Deferred in 2 seconds time with the result x * 3 
    reactor.callLater(2, d.callback, x * 3) 
    return d 

def printData(d): 
    """ 
    Data handling function to be added as a callback: handles the 
    data by printing the result 
    """ 
    raise ValueError('IIIGGAA') 
    print d 

def nextCall(d): 
    import pdb; pdb.set_trace() 
d = getDummyData(3) 

d.addErrback(nextCall).addCallback(printData).addErrback(nextCall).addCallback(nextCall) 


# manually set up the end of the process by asking the reactor to 
# stop itself in 4 seconds time 
reactor.callLater(1, reactor.stop) 
# start up the Twisted reactor (event loop handler) manually 
reactor.run() 

功能nextCall - 永遠不會調用。那麼我能找到我的ValueError?如何在扭曲的延遲迴調中捕獲異常?

謝謝。

回答

4

這是從來沒有所謂的,因爲這表示它要求反應堆停止自己在秒您的評論下的代碼實際上詢問反應堆停止自己在秒。 2秒callLater不會被調用,所以d是從來沒有發射,所以nextCall不會被調用。

也許你應該嘗試構建這個例子中不使用電抗器,只需通過調用同步推遲適當callback?你不需要反應堆發射一個簡單的Deferred,並且同步地與它們混合可以幫助你更準確地瞭解什麼時候發生了什麼。

+1

你們好,謝謝!我不忍心。我怎麼錯過了?!? – Oduvan