twisted.internet.defer.DeferredList
执行此操作:
我将一组延迟合并为一个回调。
我跟踪他们的回调的Deferred列表,并制作一个 完成后回调,列表(成功,结果) 元组,成功'是一个布尔值。
请注意,您可以在将其放入后使用延迟 DeferredList。例如,您可以禁止显示“未处理的错误” 递延'通过在推送后向Erferreds 添加errbacks消息 它们在DeferredList中,因为DeferredList不会吞下错误。 (虽然更方便的方法是简单地设置 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
(关键字参数)一个标志,表示只有一个 需要解雇回调才能调用我的回调
我正在寻找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)
答案 0 :(得分:1)
这是一种可能的方法。
from __future__ import print_function
import attr
from twisted.internet.defer import Deferred
def fireOnN(n, ds):
acc = _Accumulator(n)
for index, d in enumerate(ds):
d.addCallback(acc.one_result, index)
return acc.n_results
@attr.s
class _Accumulator(object):
n = attr.ib()
so_far = attr.ib(default=attr.Factory(dict))
done = attr.ib(default=False)
n_results = attr.ib(default=attr.Factory(Deferred))
def one_result(self, result, index):
if self.done:
return result
self.so_far[index] = result
if len(self.so_far) == self.n:
self.done = True
so_far = self.so_far
self.so_far = None
self.n_results.callback(so_far)
dx = list(Deferred().addCallback(print, i) for i in range(3))
done = fireOnN(2, dx)
done.addCallback(print, "done")
for i, d in enumerate(dx):
d.callback("result {}".format(i))
请注意,此实现不处理错误,也可能有其他缺点(比如坚持n_results
引用)。但是,基本思路是合理的:从回调中累积状态,直到达到所需的条件,然后再触发另一个延迟。
DeferredList
只会为此问题带来不必要的复杂性,其无关的功能和界面不是为解决此问题而设计的。
答案 1 :(得分:1)
这是使用DeferredSemaphore
来处理潜在竞争条件的另一种方法。一旦延迟的n
被解雇,这将立即触发,并取消其余部分。
from twisted.internet import defer
def fireAfterNthCallback(deferreds, n):
if not n or n > len(deferreds):
raise ValueError
results = {}
finished_deferred = defer.Deferred()
sem = defer.DeferredSemaphore(1)
def wrap_sem(result, index):
return sem.run(callback_result, result, index)
def cancel_remaining():
finished = [deferreds[index] for index in results.keys()]
for d in finished:
deferreds.remove(d)
for d in deferreds:
d.addErrback(lambda err: err.trap(defer.CancelledError))
d.cancel()
def callback_result(result, index):
results[index] = result
if len(results) >= n:
cancel_remaining()
finished_deferred.callback(results.values())
return result
for deferred_index, deferred in enumerate(deferreds):
deferred.addCallback(wrap_sem, deferred_index)
return finished_deferred