python requests 使用Session对象加速

Requests

会话对象

会话对象让你能够跨请求保持某些参数。它也会在同一个 Session 实例发出的所有请求之间保持 cookie, 期间使用 urllib3 的 connection pooling 功能。所以如果你向同一主机发送多个请求,底层的 TCP 连接将会被重用,从而带来显著的性能提升。

http://docs.python-requests.org/zh_CN/latest/user/advanced.html#keep-alive

 

requ = requests.session()
%timeit requests.get('http://httpbin.org/get')
1 loop, best of 3: 918 ms per loop

%timeit requ.get('http://httpbin.org/get')
The slowest run took 4.43 times longer than the fastest. This could mean that an intermediate result is being cached.
1 loop, best of 3: 458 ms per loop

ipthon 测试httpbin.org可以快一倍

 

posted @ 2017-08-02 18:50  willowj  阅读(967)  评论(0编辑  收藏  举报