1.请求1000速度排名
模块 |
速度 |
requests 不保持连接 |
1324.95 |
requests 保持连接 |
287.08 |
httpx同步 |
1350.26 |
httpx[异步]一个AsyncClient |
12.53 |
httpx[异步]每次创建AsyncClient |
26.98 |
aiohttp[异步]创建一个ClientSession |
4.49 |
aiohttp[异步]每次创建ClientSession |
8.58 |
2.requests
2-1 requests 不保持连接
import time
import requests
def make_request(n):
resp = requests.get("https://httpbin.org/get", params={'number': n})
print(n)
def main():
start_time = time.perf_counter()
for n in range(1000):
make_request(n)
end_time = time.perf_counter()
print(f"Elapsed run time: {end_time - start_time} seconds.")
if __name__ == '__main__':
main()
lapsed run time: 1324.9560645 seconds.
2-2 requests 保持连接
import time
import requests
session = requests.session()
def make_request(n):
resp = session.get("https://httpbin.org/get", params={'number': n})
print(n)
def main():
start_time = time.perf_counter()
for n in range(1000):
make_request(n)
end_time = time.perf_counter()
print(f"Elapsed run time: {end_time - start_time} seconds.")
if __name__ == '__main__':
main()
lapsed run time: 287.0817919 seconds.
3.httpx
3-1 httpx同步
import time
import httpx
def make_request(n):
resp = httpx.get("https://httpbin.org/get", params={'number': n})
print(resp.status_code)
def main():
start_time = time.perf_counter()
for n in range(1000):
make_request(n)
end_time = time.perf_counter()
print(f"Elapsed run time: {end_time - start_time} seconds.")
if __name__ == '__main__':
main()
Elapsed run time: 1350.2615917 seconds.
3-2 httpx异步
3-2-1 创建一个AsyncClient
import time
import asyncio
import httpx
async def fetch(client, n):
await client.get("https://httpbin.org/get", params={'number': n})
async def main():
async with httpx.AsyncClient() as client:
await asyncio.gather(*[fetch(client, num) for num in range(1000)])
start_time = time.perf_counter()
asyncio.get_event_loop().run_until_complete(main())
end_time = time.perf_counter()
print(f"Elapsed run time: {end_time - start_time} seconds.")
Elapsed run time: 12.536532 seconds.
3-2-2 每次新建AsyncClient
import time
import asyncio
import httpx
async def fetch(n):
async with httpx.AsyncClient() as client:
await client.get("https://httpbin.org/get", params={'number': n})
async def main():
await asyncio.gather(*[fetch(num) for num in range(1000)])
start_time = time.perf_counter()
asyncio.get_event_loop().run_until_complete(main())
end_time = time.perf_counter()
print(f"Elapsed run time: {end_time - start_time} seconds.")
Elapsed run time: 26.9852918 seconds.
4.aiohttp
4-1 创建一个ClientSession
import time
import asyncio
import aiohttp
async def fetch(session, n):
await session.get("https://httpbin.org/get", params={'number': n})
async def main():
async with aiohttp.ClientSession() as session:
await asyncio.gather(*[fetch(session, num) for num in range(1000)])
start_time = time.perf_counter()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
end_time = time.perf_counter()
print(f"Elapsed run time: {end_time - start_time} seconds.")
Elapsed run time: 4.495165500000001 seconds.
4-2 每次新建ClientSession
import time
import asyncio
import aiohttp
async def fetch(n):
async with aiohttp.ClientSession() as session:
await session.get("https://httpbin.org/get", params={'number': n})
async def main():
await asyncio.gather(*[fetch(num) for num in range(1000)])
start_time = time.perf_counter()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
end_time = time.perf_counter()
print(f"Elapsed run time: {end_time - start_time} seconds.")
Elapsed run time: 8.5865487 seconds.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现