Skip to content

Instantly share code, notes, and snippets.

@2minchul
Created January 18, 2019 03:50
Show Gist options
  • Save 2minchul/da19cf0698ffa0cf32b595eecf3b463c to your computer and use it in GitHub Desktop.
Save 2minchul/da19cf0698ffa0cf32b595eecf3b463c to your computer and use it in GitHub Desktop.
A simple example of tenacity with aiohttp
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