Re: [Twisted-Python] Retrying function calls

2009-11-01 Thread Terry Jones
Hi Blair

 Looks pretty good. To streamline the usage and make the intent more
 apparent from a brief glance at the header you might consider turning it
 into a decorator, like

Thanks for the suggestion  the pointer. I'd not seen that page.

I don't think this is well suited to decorators, at least not with the
kinds of usages I am imagining. If you decorate a function, it's done once
and for all. So anyone who calls the function gets the single
one-size-fits-all decorated behavior. I'd rather the behavior was left in
the hands of the caller. That's kind of the point: give the caller flexible
control over what happens if something goes wrong, including passing in
your custom failure handler, etc.

Maybe a hybrid approach would be more useful: write a function which,
passed a function, returns a retrying version of that function that returns
a deferred that fires when the original function has succeeded (or
ultimately failed). The result could then be passed around, called by
multiple pieces of code, etc. Hmmm...

Thanks again.

Terry

___
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python


Re: [Twisted-Python] Retrying function calls

2009-11-01 Thread Tim Allen
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.

___
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python


Re: [Twisted-Python] Retrying function calls

2009-11-01 Thread Terry Jones
Hi Tim

 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:

Ah yes, thanks a lot.

Terry

___
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python