I have been trying to speed up a batch of api requests and I couldn't figure out why this code doesn't return anything. When I do this without async functions and just using simple requests everything works but now it returns none.
import requests
import json
import asyncio
import aiohttp
competition = ['216718', '177489', '132739', '168769', '168770', '147735', '132822', '132823', '133015', '173636', '151636', '217971', '217838', '217984', '173138', '152566', '133025', '130047', '217911', '136630', '162388', '132149', '152553', '178776', '152554', '152555', '215790', '215789', '215791', '215792', '108698', '176597', '132748', '132747', '181111', '132424', '132639', '147507', '147532', '143084', '132638', '126842', '126665', '126667', '132298', '125984', '161539', '141677', '149844']
url = "https://www.maxbet.rs/restapi/offer/sr/categories/sport/S/l"
querystring1 = {"annex":"3","desktopVersion":"2.24.89","locale":"sr"}
headers1 = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0",
"Accept": "application/json, text/plain, */*",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"X-INSTANA-T": "d55204082031b541",
"X-INSTANA-S": "d55204082031b541",
"X-INSTANA-L": "1,correlationType=web;correlationId=d55204082031b541",
"DNT": "1",
"Sec-GPC": "1",
"Connection": "keep-alive",
"Referer": "https://www.maxbet.rs/leagues/S",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"TE": "trailers"
}
results=[]
def get_tasks(session):
tasks=[]
for id in competition:
url1 = "https://www.maxbet.rs/restapi/offer/sr/sport/S/league/"+id+"/mob"
tasks.append(session.get(url1,headers=headers1,params=querystring1))
return tasks
async def matches():
tasks = []
session = aiohttp.ClientSession()
tasks = get_tasks(session)
responses = await asyncio.gather(*tasks)
for response in responses:
results.append(await response.json())
await session.close()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
results = loop.run_until_complete(matches())
loop.close()
print(results)
When I tried to print the responses it just returns code 200 and nothing else.
Your code seems to be fine and should you return valid responses. But you don't return results from matches
async def matches():
tasks = []
session = aiohttp.ClientSession()
tasks = get_tasks(session)
responses = await asyncio.gather(*tasks)
for response in responses:
results.append(await response.json())
await session.close()
return results # <- should help