2011-10-28 9 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() 

function nextCall - 決して呼び出しません。私はValueErrorを見つけることができましたか?Twisted Deferredコールバックで例外を捕捉するにはどうすればよいですか?

ありがとうございました。それは秒で自分自身を停止するために原子炉を頼むと言うあなたのコメントの下のコードは、実際に秒で自分自身を停止するために原子炉を要求するため

答えて

4

は、それが呼び出されることはありませんです。 2秒のcallLaterは決して呼び出されないので、dは決して解雇されないので、nextCallは決して呼び出されません。

リアクタを使用せずにこの例を構築すると、ちょうど適切な遅延で同期してcallbackを呼び出してみてください。原子炉が簡素なDeferredを発射する必要はありませんし、それらを同期させることで正確に何が起こるかをより正確に知ることができます。

+1

よろしくお願い致します。わからない。どうやってそれを逃したの? – Oduvan

関連する問題