在python中对api请求使用多线程

在python中对api请求使用多线程,python,multithreading,api,python-multiprocessing,python-multithreading,Python,Multithreading,Api,Python Multiprocessing,Python Multithreading,对于我的项目,我需要请求一个api并将结果存储在一个列表中。但我需要给出的请求数量超过5000个,具有不同的body值。所以,这需要大量的时间来完成。有没有办法并行发送请求以快速完成流程。我尝试了一些线程代码,但我无法找到解决这个问题的方法 import requests res_list=[] l=[19821, 29674 , 41983, 40234 ,.....] # Nearly 5000 items for now and the count may increase in fut

对于我的项目,我需要请求一个api并将结果存储在一个列表中。但我需要给出的请求数量超过5000个,具有不同的body值。所以,这需要大量的时间来完成。有没有办法并行发送请求以快速完成流程。我尝试了一些线程代码,但我无法找到解决这个问题的方法

import requests
res_list=[]
l=[19821, 29674 , 41983, 40234 ,.....] # Nearly 5000 items for now  and the count may increase in future
for i in l:
    URL ="https://api.something.com/?key=xxx-xxx-xxx&job_id={0}".format(i)
    res = requests.get(url=URL)
    res_list.append(res.text)

也许,您只需要异步进行查询。诸如此类:

import asyncio

import aiohttp

NUMBERS = [1, 2, 3]


async def call():
    async with aiohttp.ClientSession() as session:
        for num in NUMBERS:
            async with session.get(f'http://httpbin.org/get?{num}') as resp:
                print(resp.status)
                print(await resp.text())


if __name__ == '__main__':
    loop = asyncio.new_event_loop()
    loop.run_until_complete(call())

你的问题是?到目前为止,它只是一句话:“我无法编程我需要的”。