Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A little experiment to handle occasional connection timeouts when using requests to connect to an external API.
#!/usr/bin/env python
import time
import random
from flask import Flask
app = Flask(__name__)
@app.route('/timeout_sometimes')
def timeout_then_go():
sleep_for = random.randint(0, 2)
print "waiting for {} seconds".format(sleep_for)
time.sleep(sleep_for)
return 'Hello'
if __name__ == '__main__':
app.run(debug=True)
#!/usr/bin/env python
import click
import requests
from requests.adapters import HTTPAdapter
@click.command()
@click.option('--retry', default=False, is_flag=True)
@click.option('--max-retries', default=None, type=int)
@click.option('--num-tests', default=20, type=int)
@click.option('--timeout', default=2, type=int)
def run_test(retry, max_retries, num_tests, timeout):
s = requests.Session()
if retry or max_retries:
max_retries = max_retries or 3
click.echo("Running with {} retries".format(max_retries))
s.mount('http://localhost:5000/', HTTPAdapter(max_retries=max_retries))
for __ in xrange(int(num_tests)):
try:
response = s.request(
method="GET",
url='http://localhost:5000/timeout_sometimes',
timeout=timeout)
except Exception as exc:
click.echo(str(exc))
else:
click.echo("Response: {}".format(response.status_code))
if __name__ == "__main__":
run_test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment