Last active
August 29, 2015 14:20
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| requests | |
| click |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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