[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Subject: Python Open-Source Snippets Newsletter


I am creating a Python newsletter showcasing useful code snippets from 
popular open-source libraries. I will also be providing a runnable demo 
link to better understand the working.

Newsletter subscription link: https://www.pythonninja.xyz/subscribe

A sample snippet from the newsletter:

HTTP Request retrying with Backoffs - Technique for retrying failed HTTP 

 From Google Maps Services Python 

Demo link: https://repl.it/@PythonNinja/requestretries

first_request_time: The time of the first request (None if no retries 
have occurred).
retry_counter: The count of retries, or zero for the first attempt.

if not first_request_time:
     first_request_time = datetime.now()

elapsed = datetime.now() - first_request_time
if elapsed > self.retry_timeout:
     raise googlemaps.exceptions.Timeout()

if retry_counter > 0:
     # 0.5 * (1.5 ^ i) is an increased sleep time of 1.5x per iteration,
     # starting at 0.5s when retry_counter=0. The first retry will occur
     # at 1, so subtract that first.
     delay_seconds = 0.5 * 1.5 ** (retry_counter - 1)

     # Jitter this value by 50% and pause.
     time.sleep(delay_seconds * (random.random() + 0.5))

Subscribe here: https://www.pythonninja.xyz/subscribe

Feedbacks and criticism are welcome.