Skip to content

Instantly share code, notes, and snippets.

@roadsideseb
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roadsideseb/e8baf68b4a0f6ea27440 to your computer and use it in GitHub Desktop.
Save roadsideseb/e8baf68b4a0f6ea27440 to your computer and use it in GitHub Desktop.
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