python异步请求http client 超时计时

自Python 推出之后async、await关键字支持异步协程之后,异步框架越来越火热,像tornado本身支持异步编程,新推出的fastapi速度和可用性都受到追捧,异步编程将是Python解决并发短板的一大利器。

异步并发实现,一是依赖框架提起的server端,同时也需要api中依赖的client同时实现异步,否则异步因一处io操作阻塞,因无法让出event loop而最终导致无法完全异步。

目前,封装较好,使用方便的http client主要有aiohttp,httpx,本文推荐使用httpx,因为在timeout计时准确性上,httpx完全能达到requests的精度,而aiohttp则有较大误差。

timeout测试

  1. requests ,准确
import requests
import time


def main():
    resp = requests.get('http://httpbin.org/get', timeout=0.01)
    result = resp.json()
    print(result)


s = time.time()
try:
    main()
except Exception as e:
    print(e)
print("cost:", time.time() - s)

"""运行结果
HTTPConnectionPool(host='httpbin.org', port=80): Max retries exceeded with url: /get (Caused by ConnectTimeoutError(, 'Connection to httpbin.org timed out. (connect timeout=0.01)'))
cost: 0.0408937931060791
"""
  1. aiohttp ,不准确
import aiohttp
import asyncio
import time


async def main():
    async with aiohttp.ClientSession() as session:
        async with session.get('http://httpbin.org/get', timeout=0.01) as resp:
            result = await resp.text()
            print(result)


s = time.time()
try:
    asyncio.run(main())
except Exception as e:
    print(e)

print("cost:", time.time() - s)
"""运行结果

cost: 0.6044762134552002
"""

  1. httpx ,准确
import httpx
import asyncio
import time


async def main():
    async with httpx.AsyncClient() as client:
        resp = await client.get('http://httpbin.org/get',timeout=0.01)
        result = resp.json()
        print(result)

s = time.time()

try:
    asyncio.run(main())
except Exception as e:
    print(e)
print("cost:",time.time() -s )

"""运行结果

cost: 0.01595759391784668
"""

为什么aiohttp不会像httpx一样准确抛出timeout计时呢?有哪位同学知道,帮忙解答一下,谢谢!

你可能感兴趣的:(Python后端,python)