[Twisted-Python] Retrying function calls
Tim Allen
screwtape at froup.com
Sun Nov 1 19:11:32 EST 2009
On Sun, Nov 01, 2009 at 05:53:31PM +0100, Terry Jones wrote:
> def simpleBackoffIterator(maxResults=10, maxDelay=5.0, now=True,
> initDelay=0.01, incFunc=None):
> assert maxResults > 0
> remaining = maxResults
> delay = initDelay
> incFunc = incFunc or partial(mul, 2.0)
>
> if now:
> yield 0.0
> remaining -= 1
> while True:
> if remaining == 0:
> raise StopIteration
> yield (delay if delay < maxDelay else maxDelay)
> delay = incFunc(delay)
> remaining -= 1
Since this is a generator function, it will automatically raise
StopIteration once control-flow falls off the end of the function, so
your while-loop could just be written:
while remaining > 0:
yield (delay if delay < maxDelay else maxDelay)
delay = incFunc(delay)
remaining -= 1
...making the function of the "remaining" counter just a little more
explicit.
More information about the Twisted-Python
mailing list