Skip to content

Instantly share code, notes, and snippets.

@oxlade39
Created November 5, 2018 07:03
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 oxlade39/4a699996a3d987d5e1ffc087fc76d788 to your computer and use it in GitHub Desktop.
Save oxlade39/4a699996a3d987d5e1ffc087fc76d788 to your computer and use it in GitHub Desktop.
import aiohttp
import asyncio
async def fetch(session, url):
async with session.get(url) as response:
return await response.text()
async def main():
async with aiohttp.ClientSession() as session:
results = [wait_for_code(404, session, 'http://python.org') for i in range(2)]
[print(result) for result in await asyncio.gather(*results)]
async def wait_for_code(code, session, url, step=1, retries=10):
async with session.get(url) as response:
count = retries
while(count > 0):
if response.status != code:
count = count - 1
print("no match after {0} attempts. {1} attempts left".format(retries - count, count))
await asyncio.sleep(step)
else:
return await response.text()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment