目前使用 requests 库去进行一些 api 的访问,惊讶的发现 requests 库速度相较于 urllib3 慢非常多
$ url = "https://httpbin.org"
$ %timeit r = requests.get(url)
119 ms ± 42.5 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
$ %timeit r = httpx.get(url)
%117 ms ± 17.6 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
$ http = urllib3.PoolManager()
$ %timeit response = http.request("GET", url)
The slowest run took 6.87 times longer than the fastest. This could mean that an intermediate result is being cached.
10.6 ms ± 6.91 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)
其中还尝试了 session 等方法,貌似也没有变快,上面的是多次的测试,我还做了一次请求的测试,看上去也是很慢
看了看源码和原理,想象不出来这个速度差来自哪里?
需求就是请求网址需要快,但不一定需要多次请求
1
annoygaga OP 这里也试了带 json 数据的 post 方法,结果也是一样的
|
2
WildCat 2023-11-26 23:22:57 +08:00
感觉 urllib3.PoolManager() 是一个 connection pool ?
有 re-use 现成的 TCP 链接 |
3
ysc3839 2023-11-26 23:24:21 +08:00 via Android
控制变量都没做好,requests 是直接 get 的,utllib3 怎么就用了 PoolManager ?
|
6
annoygaga OP 而且单次请求的速度,试了几次,也是 urllib3 快一些
|
7
annoygaga OP ```
%timeit r = sess.get(url) The slowest run took 16.83 times longer than the fastest. This could mean that an intermediate result is being cached. 20.8 ms ± 21.9 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) ``` 补一下 session 的结果,会快一些 但是我的需求其实是单次请求快(不求复用链接。。。 |
8
009694 2023-11-26 23:28:18 +08:00
requests 是 urllib3 的封装。 你怎么可能期望更高层的封装比底层调用快?
|
10
justFxxk2060 2023-11-26 23:42:55 +08:00
import requests
session = requests.Session() url = "https://httpbin.org" %timeit session.get(url) 这样试试 @ysc3839 是对的 |
11
justFxxk2060 2023-11-26 23:49:30 +08:00
Session 单次虽然没有意义,但是也能复用底层 TCP 连接
比如你先运行 http = urllib3.PoolManager() 然后运行 session.get(url) 指不定也会更快一些 |
12
zeusho871 2023-11-27 00:02:05 +08:00
119 117 10.6ms?相差 10 倍 这么离谱的吗
|
13
liprais 2023-11-27 00:08:48 +08:00
"The slowest run took 6.87 times longer than the fastest. This could mean that an intermediate result is being cached."
这几行字是看不见么? |
16
u823tg 2023-11-27 01:25:13 +08:00
不都用 httpx 了吗还是我好久没写 python 了,又转回到 requests 风向了
|
17
annoygaga OP @u823tg httpx 也测了,参考上面,with client 下和 requests 的 session 差不多
|
18
mengzhuo 2023-11-27 09:23:48 +08:00
大概率是 cache ,每次请求带个随机数参数试试?
|
19
linhua 2023-11-27 09:51:23 +08:00
单次时间,那就不要用 timeit 测量,timeit 是 多次请求 平均的时间。
|
20
Alias4ck 2023-11-27 10:26:28 +08:00
|
23
louisxxx 2023-11-28 05:54:17 +08:00 via iPhone
本身如此啊,你得为方便牺牲性能
|
24
bbxiong 2023-12-04 11:34:27 +08:00
关注
|