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

1 回答 1

4

它从来没有被调用过,因为您评论下的代码要求反应堆在4秒内自行停止,实际上是要求反应堆在1秒内自行停止。2 秒callLater永远不会被调用,所以d永远不会被触发,所以nextCall永远不会被调用。

也许您应该尝试在不使用反应器的情况下构建这个示例,只需callback同步调用适当的延迟?你不需要反应堆来触发一个简单Deferred的同步处理它们可以帮助你更准确地了解什么时候会发生什么。

于 2011-10-28T11:52:37.720 回答