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.
标签:run,aiohttp,get,end,time,import,requests,main,httpx
From: https://www.cnblogs.com/lxd670/p/17034735.html