Skip to content

Instantly share code, notes, and snippets.

@KarimullinArthur
Created January 19, 2024 11:26
Show Gist options
  • Save KarimullinArthur/2170efa7ee8f8500b58d6001c9e41363 to your computer and use it in GitHub Desktop.
Save KarimullinArthur/2170efa7ee8f8500b58d6001c9e41363 to your computer and use it in GitHub Desktop.
aiohttp_vs_requests.py
import aiohttp
import asyncio
import requests
import time
async def aio():
URL = 'https://cleanuri.com/api/v1/shorten'
links = ['https://artlin.codeberg.page/contacts.html'] * 10
async with aiohttp.ClientSession() as session:
tasks = []
for link in links:
data = {'url': link}
tasks.append(session.post(url=URL, data=data))
result = await asyncio.gather(*tasks)
return result
def req():
URL = 'https://cleanuri.com/api/v1/shorten'
links = ['https://artlin.codeberg.page/contacts.html'] * 10
result = []
for link in links:
data = {'url': link}
result.append(requests.post(url=URL, data=data))
return result
print('AioHTTP')
for i in range(3):
start_time = time.time()
asyncio.run(aio())
end_time = time.time()
elapsed_time = end_time - start_time
print(f"Elapsed time: {elapsed_time}")
print('\nRequests')
for i in range(3):
start_time = time.time()
req()
end_time = time.time()
elapsed_time = end_time - start_time
print(f"Elapsed time: {elapsed_time}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment