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

functionnextCall-決して呼び出さない。それで、ValueErrorを見つけることができましたか?

ありがとう。

4

1 に答える 1

4

あなたのコメントの下にあるコードは、リアクターに4秒で停止するように要求しているため、実際にはリアクターに1秒で停止するように要求しているため、呼び出されません。2 秒callLaterは呼び出されないためd、起動されず、nextCall呼び出されません。

callback適切な deferred を同期的に呼び出すだけで、reactor を使用せずにこの例を構築してみてはいかがでしょうか? リアクターを単純に起動する必要はなくDeferred、それらを同期的にいじることで、正確にいつ何が起こるかについてより正確なアイデアを得ることができます。

于 2011-10-28T11:52:37.720 に答える