http异步请求

信息安全不简单鸭 2024-09-02 21:56:40

现在AI大模型越来越火了,我们搞安全的也要学习大模型,最近拿到了一个大模型调用接口,发现普通的http请求响应非常慢,请求一次差不多5~10秒才能返回,于是试用了http异步接口,效率一下子提高了100倍。

python3 http 同步请求100次,耗时接近102秒python3 http 异步请求100次,不到6秒,性能提高了17倍,实际上请求次数越多,性能提升越明显

ailx10

网络安全优秀回答者

网络安全硕士

去咨询

python3 http 同步请求100次,耗时接近102秒

# python3 http 同步请求import requestsimport timedef fetch_data(url): response = requests.get(url) return response.textdef main(): url = 'http://ctf.hackbiji.top' # 我自己的网站 total_requests = 100 # 请求次数 start_time = time.time() # 记录开始时间 responses = [fetch_data(url) for _ in range(total_requests)] end_time = time.time() # 记录结束时间 # 处理响应 for i, response in enumerate(responses[:5]): print(f"Response {i + 1}...") print(f"Total responses: {len(responses)}") total_time = end_time - start_time print(f"Total time taken: {total_time} seconds")# 运行主函数main()

python3 http 异步请求100次,不到6秒,性能提高了17倍,实际上请求次数越多,性能提升越明显

python3.7版本

# python3.7 http 异步请求import asyncioimport aiohttpimport timeasync def fetch_data(session, url): async with session.get(url) as response: return await response.text()async def main(): url = 'http://ctf.hackbiji.top' # 我自己的网站 total_requests = 100 # 请求次数 async with aiohttp.ClientSession() as session: tasks = [] for _ in range(total_requests): task = asyncio.create_task(fetch_data(session, url)) tasks.append(task) start_time = time.time() # 记录开始时间 responses = await asyncio.gather(*tasks) end_time = time.time() # 记录结束时间 # 处理响应 for i, response in enumerate(responses[:5]): print(f"Response {i + 1}...") print(f"Total responses: {len(responses)}") total_time = end_time - start_time print(f"Total time taken: {total_time} seconds")# 运行主函数asyncio.run(main())

python3.6版本

# python3.6 http 异步请求import asyncioimport aiohttpasync def fetch_data(url): async with aiohttp.ClientSession() as session: async with session.get(url) as response: return response.status, await response.text()async def main(): url = 'http://ctf.hackbiji.top' # 我自己的网站 tasks = [] # 创建100个任务 for i in range(100): task = asyncio.ensure_future(fetch_data(f"{url}")) tasks.append(task) # 并发执行任务 responses = await asyncio.gather(*tasks) # 检查响应状态码 for i, (status_code, response_text) in enumerate(responses, 1): if status_code == 200: print(f"Request {i}: Request successful! Response ...") # 打印部分响应内容 else: print(f"Request {i}: Request failed with status code: {status_code}")# 运行异步函数loop = asyncio.get_event_loop()loop.run_until_complete(main())

python3.6面向对象编程

# python3.6 http 异步请求import asyncioimport aiohttpclass AsyncRequester: def __init__(self, url): self.url = url async def fetch_data(self): async with aiohttp.ClientSession() as session: async with session.get(self.url) as response: return response.status, await response.text() async def make_requests(self, num_requests): tasks = [] # 创建 num_requests 个任务 for i in range(num_requests): task = asyncio.ensure_future(self.fetch_data()) tasks.append(task) # 并发执行任务 responses = await asyncio.gather(*tasks) # 检查响应状态码 for i, (status_code, response_text) in enumerate(responses, 1): if status_code == 200: print(f"Request {i}: Request successful! Response:...") # 打印部分响应内容 else: print(f"Request {i}: Request failed with status code: {status_code}")# 实例化类并运行async def main(): requester = AsyncRequester('http://ctf.hackbiji.top') # 我自己的网站 await requester.make_requests(100) # 发送100个请求# 运行异步函数loop = asyncio.get_event_loop()loop.run_until_complete(main())

发布于 2024-01-05 15:16・IP 属地美国

0 阅读:20