一个^{
I combine a group of deferreds into one callback.
I track a list of Deferreds for their callbacks, and make a single callback when they have all completed, a list of (success, result) tuples, 'success' being a boolean.
Note that you can still use a Deferred after putting it in a DeferredList. For example, you can suppress 'Unhandled error in Deferred' messages by adding errbacks to the Deferreds after putting them in the DeferredList, as a DeferredList won't swallow the errors. (Although a more convenient way to do this is simply to set the consumeErrors flag)
def __init__(self, deferredList, fireOnOneCallback=0, fireOnOneErrback=0, consumeErrors=0): (source)
overrides twisted.internet.defer.Deferred.__init__
Initialize a DeferredList.
Parameters deferredList The list of deferreds to track. (type: list of Deferreds )
fireOnOneCallback (keyword param) a flag indicating that only one callback needs to be fired for me to call my callback
fireOnOneErrback (keyword param) a flag indicating that only one errback needs to be fired for me to call my errback
consumeErrors (keyword param) a flag indicating that any errors raised in the original deferreds should be consumed by this DeferredList. This is useful to prevent spurious warnings being logged.
具体来说:
fireOnOneCallback
(keyword param) a flag indicating that only one callback needs to be fired for me to call my callback
我正在寻找像fireOnOneCallback=True
这样的行为,但我希望触发n
回调。我试过这么做,但已经变得一团糟了。我相信有更好的办法。你知道吗
def _get_fired_index(deferred_list):
for index, (success, value) in enumerate(deferred_list):
if success:
return index
raise ValueError('No deferreds were fired.')
def _fire_on_other_callback(already_fired_index, deferred_list, callback, ):
dlist_except_first_fired = (
deferred_list[:already_fired_index]
+ deferred_list[already_fired_index + 1:]
)
dlist2 = DeferredList(dlist_except_first_fired, fireOnOneCallback=True)
dlist2.addCallback(callback, deferred_list)
def _fire_on_two_callbacks(deferreds, callback, errback):
dlist1 = DeferredList(deferreds, fireOnOneCallback=True)
dlist1.addCallback(_get_fired_index)
dlist1.addCallback(_fire_on_other_callback, deferreds, callback, errback)
这里有一种可能的方法。你知道吗
注意,这个实现不处理错误,可能还有其他缺点(比如保留
n_results
引用)。然而,基本思想是正确的:从回调中积累状态,直到达到所需的条件,然后触发另一个延迟状态。你知道吗DeferredList
只会给这个问题带来不必要的复杂性,因为它的无关特性和接口不是为解决这个问题而设计的。你知道吗下面是另一种使用
DeferredSemaphore
来处理潜在竞争条件的方法。这将在n
延迟者被触发后立即触发,并取消其余的延迟者。你知道吗相关问题 更多 >
编程相关推荐