Last active
June 1, 2024 19:29
-
-
Save igorzakhar/9d120d7a192eac2e4dc160eac65a0eeb to your computer and use it in GitHub Desktop.
Get response status code using asyncio/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 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why do you need to wrap the get_status call in an ensure_future? Seems redundant to the gather you are doing later, no?