【问题标题】:How to catch exception in twisted Deferred callback?如何在扭曲的延迟回调中捕获异常?
【发布时间】:2023-03-13 07:56:02
【问题描述】:
    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 吗?

谢谢。

【问题讨论】:

    标签: python twisted deferred


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-08
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多