Skip to content

Instantly share code, notes, and snippets.

@billychan
Forked from echohack/polling.py
Created April 6, 2022 08:28
Show Gist options
  • Save billychan/a1d0e762e05432636cb8233acc67c144 to your computer and use it in GitHub Desktop.
Save billychan/a1d0e762e05432636cb8233acc67c144 to your computer and use it in GitHub Desktop.
class APIWrapper:
#snippet...
def poll_api(self, tries, initial_delay, delay, backoff, success_list, apifunction, *args):
time.sleep(initial_delay)
for n in range(tries):
try:
status = self.get_status()
if status not in success_list:
polling_time = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
print("{0}. Sleeping for {1} seconds.".format(polling_time, delay))
time.sleep(delay)
delay *= backoff
else:
return apifunction(*args)
except socket.error as e:
print("Connection dropped with error code {0}".format(e.errno))
raise ExceededRetries("Failed to poll {0} within {1} tries.".format(apifunction, tries))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment