My code sends async request to a api but it’s not really faster than my code with sync request. Currently it sends around 50-60 requests and it takes around 1 minute. What am I doing wrong?
#gets total pages
def Page_count():
response = requests.get('https://api.hypixel.net/v2/skyblock/auctions?page=1').json()
total_pages = response["totalPages"]
return total_pages
# Asynchronous function for getting a single page
async def get_page(session, url):
async with session.get(url) as response:
page = await response.json()
#says what page number at what time got scraped
print("Got Page: " + str(page['page'] + 1))
return page['auctions']
# Asynchronously get all pages with aiohttp
async def main(total_pages):
async with aiohttp.ClientSession() as session:
# Create list of tasks (API calls)
tasks = []
for page_number in range(total_pages):
url = f"https://api.hypixel.net/v2/skyblock/auctions?page={page_number}"
tasks.append(asyncio.ensure_future(get_page(session, url)))
# Then gather and return all responses as a list
print("Requesting Pages!")
pages_data = await asyncio.gather(*tasks)
print('arranging list')
all_auctions = [auction for page in pages_data for auction in page]
return all_auctions
total_pages = Page_count() #count pages
auctions = asyncio.run(main(total_pages))
print('there are ' + str(len(auctions)) + ' auctions') #prints how many auctions there are