代码
import asyncio
import time
import requests
import aiohttp
import httpx
url = "https://www.baidu.com"
def use_requests():
begin = time.time()
session = requests.Session()
for _ in range(100):
session.get(url)
print(time.time() - begin, "use_requests")
async def a_make_requests(url, client):
async with client.get(url) as resp:
await resp.text()
async def use_aiohttp():
begin = time.time()
async with aiohttp.ClientSession() as cli:
tasks = [asyncio.create_task(a_make_requests(url, cli)) for _ in range(100)]
await asyncio.wait(tasks)
print(time.time() - begin, "use_aiohttp")
async def use_httpx():
begin = time.time()
async with httpx.AsyncClient() as cli:
tasks = [asyncio.create_task(cli.get(url)) for _ in range(100)]
await asyncio.wait(tasks)
print(time.time() - begin, "use_httpx")
if __name__ == '__main__':
use_requests()
asyncio.get_event_loop().run_until_complete(use_aiohttp())
asyncio.get_event_loop().run_until_complete(use_httpx())
结果