我有一个问题,想出一个简单的扭曲python代码。 根据我在文档中的红色,代码here应该可以在没有未处理错误的情况下工作。
我明白了:
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,其中延迟子项的结果需要“等待”,以便父级可以继续这个结果。
答案 0 :(得分:1)
来自the documentation of 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.
您提供给d1
(示例代码中为d
)的错误会传递到d2
(示例代码中为l
)。这意味着d2
/ l
最终会出现错误,该错误会传递给第一个也是唯一的错误new_f
。 new_f
返回其参数,将d2
/ l
留下错误结果,但不会处理。
如果您的意思是new_f
来处理失败,那么您应该让它''不'返回失败。来自the Deferred
howto:
If the errback does returns a Failure or raise an exception, then that is passed
to the next errback, and so on.
答案 1 :(得分:1)
在this优秀帖子中,使用chainDeferred
时可能出现的潜在问题很少。所以当使用这种方法时,应该小心。