如何在Python中使用aiohttp和async函数来加速API请求?

问题描述 投票:0回答:1

我一直在尝试加快一批 api 请求的速度,但我不明白为什么这段代码不返回任何内容。当我在没有异步函数且仅使用简单请求的情况下执行此操作时,一切正常,但现在它不返回任何内容。

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)

当我尝试打印响应时,它只返回代码 200,没有其他任何内容。

python async-await request python-asyncio aiohttp
1个回答
0
投票

您的代码似乎没问题,您应该返回有效的响应。但您不会从

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
© www.soinside.com 2019 - 2024. All rights reserved.