def loop_until(predicate, timeout=None, message="task"):
"""
Call predicate every second, until it returns something ``Truthy``.
:param predicate: Callable returning termination condition.
:type predicate: 0-argument callable returning a Deferred.
:return: A ``Deferred`` firing with the first ``Truthy`` response from
``predicate``, or, if predicate didn't fire truthfully within the
timeout, raise TimeoutError().
"""
d = maybeDeferred(predicate)
then = time.time()
def loop(result):
if timeout and time.time() - then > timeout:
raise TimeoutError()
if not result:
print "Retrying %s given result %r..." % (message, result)
d = deferLater(reactor, 1.0, predicate)
d.addCallback(loop)
return d
return result
d.addCallback(loop)
return d
评论列表
文章目录