def loop_until_success(predicate, timeout=None, message="task"):
"""
Call predicate every second, until it fires a non-failed Deferred, or hits
the timeout.
:param predicate: Callable returning termination condition.
:type predicate: 0-argument callable returning a Deferred.
:return: A ``Deferred`` firing with the first non-failed Deferred from
``predicate``, or, if predicate didn't fire with non-``Failure``-y
thing within the timeout, returns the ``Failure``.
"""
d = maybeDeferred(predicate)
then = time.time()
def loop(failure):
if timeout and time.time() - then > timeout:
# propogate the failure
return failure
print "Retrying %s given result %r..." % (message, failure.getErrorMessage())
d = deferLater(reactor, 1.0, predicate)
d.addErrback(loop)
return d
d.addErrback(loop)
return d
评论列表
文章目录