gpt4 book ai didi

python - Twisted chainDeferred 未按预期工作

转载 作者:行者123 更新时间:2023-12-01 05:52:49 25 4
gpt4 key购买 nike

我在弄清楚有点简单的扭曲 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)

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

最佳答案

来自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 最终会出现错误,该错误会传递给其第一个也是唯一的 errback new_fnew_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.

关于python - Twisted chainDeferred 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13587549/

25 4 0
文章推荐: jquery - 在 内获取 #document 内的元素