Skip to content

Instantly share code, notes, and snippets.

@igorzakhar
Last active June 1, 2024 19:29
Show Gist options
  • Save igorzakhar/9d120d7a192eac2e4dc160eac65a0eeb to your computer and use it in GitHub Desktop.
Save igorzakhar/9d120d7a192eac2e4dc160eac65a0eeb to your computer and use it in GitHub Desktop.
Get response status code using asyncio/aiohttp
import sys
import time
import asyncio
import itertools
import aiohttp
async def spin(msg): # <2>
write, flush = sys.stdout.write, sys.stdout.flush
for char in itertools.cycle('|/-\\'):
status = char + ' ' + msg
write(status)
flush()
write('\x08' * len(status))
try:
await asyncio.sleep(.1)
except asyncio.CancelledError:
break
write(' ' * len(status) + '\x08' * len(status))
async def get_status(url, session):
async with session.get(url) as response:
return url, response.status
async def run(urls, loop):
tasks = []
spinner = asyncio.ensure_future(spin('waiting'))
async with aiohttp.ClientSession(loop=loop) as session:
for url in urls:
task = asyncio.ensure_future(get_status(url, session))
tasks.append(task)
responses = await asyncio.gather(*tasks)
spinner.cancel()
return responses
if __name__ == '__main__':
urls = [ 'http://pudelek.pl/', 'http://relarn.ru',
'http://msu.ru', 'http://россия.рф', 'http://msu.ru', 'https://pagesjaunes.fr', 'http://edu-it.ru', 'http://infojournal.ru',
'http://onyx.ru', 'http://prosv.ru', 'http://titul.ru',
'http://lequipe.fr', 'http://1und1.de',
'http://chomikuj.pl', ]
s = time.time()
loop = asyncio.get_event_loop()
res = loop.run_until_complete(run(urls, loop))
for i, j in res:
print(i, j)
t = time.time() - s
print(t)
loop.close()
@kamikaz1k
Copy link

Why do you need to wrap the get_status call in an ensure_future? Seems redundant to the gather you are doing later, no?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment