1

我在弄清楚一个有点简单的扭曲 python 代码时遇到了问题。根据我在文档中的红色,这里的代码应该可以在没有未处理错误的情况下工作。

我明白了:

HELLO!
HANDLED!
HANDLED 2!
Unhandled error in Deferred:
Unhandled Error
Traceback (most recent call last):
  File "package_tester.py", line 31, in <module>
    a().callback(2)
  File "/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py", line 368, in callback
    self._startRunCallbacks(result)
  File "/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py", line 464, in _startRunCallbacks
    self._runCallbacks()
--- <exception caught here> ---
  File "/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py", line 551, in _runCallbacks
    current.result = callback(current.result, *args, **kw)
  File "package_tester.py", line 5, in c
    raise Exception()
exceptions.Exception:

链式延迟的失败不是传递给 end() errback 吗?

出于某种原因,我无法在 Bula 的帖子下方发表评论,
我设法通过简单地添加来绕过“unexpected1.py”的行为

@defer.inlineCallbacks
def sync_deferred(self, result, deferred):
        """
        Wait for a deferred to end before continuing.
        @param deferred: deferred which will be waited to finish so the chain
        can continue.
        @return: result from the deferred.
        """
        r = yield deferred
        defer.returnValue(r)

每个chainDeferred 之后的sync_deferred,其中需要“等待”子延迟的结果,以便父可以继续此结果。

4

2 回答 2

1

以下文档Deferred.chainDeferred

When you chain a deferred d2 to another deferred d1 with d1.chainDeferred(d2),
you are making d2 participate in the callback chain of d1. Thus any event that
fires d1 will also fire d2.

您提供给d1d在您的示例代码中)的错误将传递给d2l在您的示例代码中)。这意味着d2/l以错误结束,该错误被传递给它的第一个也是唯一一个 errback new_f,. new_f返回它的参数,留下d2/l一个错误结果,它没有被处理。

如果你打算new_f处理失败,那么你应该让它“不”返回失败。从howto_Deferred

If the errback does returns a Failure or raise an exception, then that is passed
to the next errback, and so on.
于 2012-11-27T17:02:40.677 回答
1

这篇出色的文章中,很少有使用chainDeferred. 所以在使用这种方法的时候一定要小心。

于 2012-11-27T22:27:24.963 回答