[Assorted-commits] SF.net SVN: assorted: [689] python-commons/trunk/src/commons/networking.py
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-04-29 23:00:45
|
Revision: 689 http://assorted.svn.sourceforge.net/assorted/?rev=689&view=rev Author: yangzhang Date: 2008-04-29 16:00:50 -0700 (Tue, 29 Apr 2008) Log Message: ----------- added retry_exp_backoff() Modified Paths: -------------- python-commons/trunk/src/commons/networking.py Modified: python-commons/trunk/src/commons/networking.py =================================================================== --- python-commons/trunk/src/commons/networking.py 2008-04-29 05:18:57 UTC (rev 688) +++ python-commons/trunk/src/commons/networking.py 2008-04-29 23:00:50 UTC (rev 689) @@ -6,6 +6,7 @@ """ import os, sys +from time import * class NoMacAddrError( Exception ): pass @@ -37,3 +38,28 @@ raise NoMacAddrError return mac +def retry_exp_backoff(initial_backoff, multiplier, func): + """ + Repeatedly invoke L{func} until it succeeds (returns non-None), with + exponentially growing backoff delay between each try. + + @param initial_backoff: The initial backoff. + @type initial_backoff: float + + @param multiplier: The amount by which the backoff is multiplied on each + failure. + @type multiplier: float + + @param func: The zero-argument function to be invoked that returns True on + success and False on failure. + @type func: function + + @return: The result of the function + """ + backoff = initial_backoff + while True: + res = func() + if res is not None: return res + print 'backing off for', backoff + sleep(backoff) + backoff = multiplier * backoff This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |