Created
January 18, 2019 03:50
-
-
Save 2minchul/da19cf0698ffa0cf32b595eecf3b463c to your computer and use it in GitHub Desktop.
A simple example of tenacity with aiohttp
This file contains 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
import asyncio | |
import random | |
import logging | |
import aiohttp | |
import tenacity | |
logging.basicConfig(level=logging.DEBUG) | |
@tenacity.retry(stop=tenacity.stop_after_attempt(10), wait=tenacity.wait_fixed(.5), | |
after=tenacity.after_log(logging, logging.DEBUG)) | |
async def random_delay_request(n): | |
logging.info('request {}'.format(n)) | |
delay = random.randint(0, 5) | |
async with aiohttp.ClientSession() as session: | |
async with session.get('http://httpbin.org/delay/{}'.format(delay), timeout=4) as response: | |
return await response.text() | |
if __name__ == '__main__': | |
loop = asyncio.get_event_loop() | |
jobs = [random_delay_request(n) for n in range(100)] | |
loop.run_until_complete(asyncio.gather(*jobs)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this works, then blocks the event loop if it fails