I'm trying to use Python's asyncio module (for the first time) to call a URL multiple times. Asyncio is deemed as an alternative to threads so I am thinking of using it. But I'm unable to
a) Write concise code. I have to repeat the same code multiple times[code below]
b) I'm not achieving any performance benefit [time taken is similar to synchronously calling external API]
import requests
import time
import asyncio
start_time = time.time()
async def get_uuid5():
r = requests.get("https://www.uuidgenerator.net/api/version1")
print (r.text)
async def get_uuid4():
await get_uuid5()
r = requests.get("https://www.uuidgenerator.net/api/version1")
print (r.text)
async def get_uuid3():
await get_uuid4()
r = requests.get("https://www.uuidgenerator.net/api/version1")
print (r.text)
async def get_uuid2():
await get_uuid3()
r = requests.get("https://www.uuidgenerator.net/api/version1")
print (r.text)
async def get_uuid():
await get_uuid2()
r = requests.get("https://www.uuidgenerator.net/api/version1")
print (r.text)
loop = asyncio.get_event_loop()
loop.run_until_complete(get_uuid())
loop.close()
end_time = time.time()
print("time taken: ", end_time - start_time)
What am I missing? Is my implementation faulty?