Created
January 30, 2017 12:14
-
-
Save rutsky/6b2b891dff399536ce98c3d7445e39c7 to your computer and use it in GitHub Desktop.
Example of backoff usage with asyncio
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 logging | |
import aiohttp | |
import backoff | |
@backoff.on_exception(backoff.expo, | |
aiohttp.errors.ClientError, | |
max_tries=8) | |
async def get_url(url): | |
async with aiohttp.ClientSession() as session: | |
async with session.get(url) as response: | |
return await response.text() | |
@backoff.on_exception(backoff.expo, | |
aiohttp.errors.ClientError, | |
max_tries=8) | |
@asyncio.coroutine | |
def get_url_py34(url): | |
with aiohttp.ClientSession() as session: | |
response = yield from session.get(url) | |
try: | |
return (yield from response.text()) | |
except Exception: | |
response.close() | |
raise | |
finally: | |
yield from response.release() | |
format_string = '%(asctime)-15s %(name)s %(levelname)s: %(message)s' | |
logging.basicConfig(format=format_string, level=logging.DEBUG) | |
url = 'http://python.org/' | |
#url = 'http://localhost:34534/' | |
loop = asyncio.get_event_loop() | |
print(loop.run_until_complete(get_url_py34(url))[:100]) | |
print(loop.run_until_complete(get_url(url))[:100]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment