TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Backoff: Python function decorators for configurable backoff and retry

49 pointsby mockoabout 7 years ago

3 comments

brycedabout 7 years ago
Seems very similar to the riprova library. I&#x27;ve found it very useful for asyncio web scrapers.<p><a href="http:&#x2F;&#x2F;riprova.readthedocs.io&#x2F;en&#x2F;latest&#x2F;" rel="nofollow">http:&#x2F;&#x2F;riprova.readthedocs.io&#x2F;en&#x2F;latest&#x2F;</a> <a href="https:&#x2F;&#x2F;github.com&#x2F;h2non&#x2F;riprova" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;h2non&#x2F;riprova</a>
shooabout 7 years ago
Cool. If I were maintaining build&#x2F;deploy nonsense written using Python, this could be quite helpful. A lot of the build&#x2F;deploy stuff I maintain is -alas- written as Groovy scripts for Jenkins, and I&#x27;ve hand-rolled a similar, simpler wrapper construct. It started up being used to place, and now it&#x27;s probably used in a dozen places throughout various build, test and deploy scripts.<p>Usage:<p><pre><code> retry_unreliable(3){ sh &quot;essential_but_unreliable_thing --failure-probability 0.01&quot; } </code></pre> Definition:<p><pre><code> def retry_unreliable(int max_attempts, Closure func) { def error = null assert max_attempts &gt; 0 def delay_initial = 8.0f def delay_multiplier = 2.0f def delay = delay_initial for (attempt = 0; attempt &lt; max_attempts; attempt++) { if (error) { sleep(delay) delay = delay_multiplier * delay } try { func.call() error = null break } catch (e) { error = e println(&quot;Unreliable operation failed during attempt ${attempt+1}&#x2F;${max_attempts}:&quot;) println(e.toString()); } } if (error) { println(&quot;Unreliable operation failed repeatedly for ${max_attempts}&#x2F;${max_attempts} attempts. Gave up&quot;) throw error } } </code></pre> Sometimes doing something like this isn&#x27;t necessarily the right course of action -- e.g. if an external service you depend on is struggling to reply to your requests because of load, hammering it with additional requests when it starts to fail isn&#x27;t the right solution. But quite a lot of time ladling on stupid retry logic turns an irritating rare failure case into a non-issue.
nsouthabout 7 years ago
Same as <a href="https:&#x2F;&#x2F;pypi.python.org&#x2F;pypi&#x2F;retry" rel="nofollow">https:&#x2F;&#x2F;pypi.python.org&#x2F;pypi&#x2F;retry</a> no?