第一个HTTPS请求比其余请求花费更多时间

时间:2017-08-20 15:14:08

标签: python http networking https python-requests

我正在制作一个需要进行快速HTTP API调用的python应用程序。 我使用python请求包在HTTPS上进行API调用。 我注意到第一次请求总是比其他请求花费更多时间。 这不是因为建立HTTP连接,因为requests.get()在每次调用时都建立了一个新连接(带有日志的已检查请求)。

我使用Wireshark调查了我的网络,虽然我发现第一个请求比其他请求花费的时间更长,但差别并不大。

我还注意到即使我第一次调用host1而所有其余的调用都发送到host2,对host1的请求比所有后续请求花费的时间要多得多。这向我保证,该问题与与主机建立某种联系无关。

我使用ping命令检查了并没有在第一个和后续的ping请求之间找到这样的区别。

是否与操作系统有关,请求在第一个请求或其他内容上进行初始设置?

我的python代码:

import requests
import time
import logging


logging.basicConfig(level=logging.DEBUG)

url = "https://bittrex.com/api/v1.1/public/getorderbook?market=BTC-LTC&type=both"

for i in range(10):
    start = time.time()

    requests.get(url)

    end = time.time()

    print('Time: {}. index: {}'.format(end - start, i))

输出:

DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): bittrex.com
DEBUG:urllib3.connectionpool:https://bittrex.com:443 "GET /api/v1.1/public/getorderbook?market=BTC-LTC&type=both HTTP/1.1" 200 46568
Time: 0.8292889595031738. index: 0
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): bittrex.com
DEBUG:urllib3.connectionpool:https://bittrex.com:443 "GET /api/v1.1/public/getorderbook?market=BTC-LTC&type=both HTTP/1.1" 200 46568
Time: 0.14321112632751465. index: 1
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): bittrex.com
DEBUG:urllib3.connectionpool:https://bittrex.com:443 "GET /api/v1.1/public/getorderbook?market=BTC-LTC&type=both HTTP/1.1" 200 46568
Time: 0.10214948654174805. index: 2
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): bittrex.com
DEBUG:urllib3.connectionpool:https://bittrex.com:443 "GET /api/v1.1/public/getorderbook?market=BTC-LTC&type=both HTTP/1.1" 200 46568
Time: 0.10616683959960938. index: 3
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): bittrex.com
DEBUG:urllib3.connectionpool:https://bittrex.com:443 "GET /api/v1.1/public/getorderbook?market=BTC-LTC&type=both HTTP/1.1" 200 46568
Time: 0.1061558723449707. index: 4
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): bittrex.com
DEBUG:urllib3.connectionpool:https://bittrex.com:443 "GET /api/v1.1/public/getorderbook?market=BTC-LTC&type=both HTTP/1.1" 200 46568
Time: 0.09714269638061523. index: 5
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): bittrex.com
DEBUG:urllib3.connectionpool:https://bittrex.com:443 "GET /api/v1.1/public/getorderbook?market=BTC-LTC&type=both HTTP/1.1" 200 46568
Time: 0.0861358642578125. index: 6
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): bittrex.com
DEBUG:urllib3.connectionpool:https://bittrex.com:443 "GET /api/v1.1/public/getorderbook?market=BTC-LTC&type=both HTTP/1.1" 200 46568
Time: 0.08713865280151367. index: 7
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): bittrex.com
DEBUG:urllib3.connectionpool:https://bittrex.com:443 "GET /api/v1.1/public/getorderbook?market=BTC-LTC&type=both HTTP/1.1" 200 46568
Time: 0.09714365005493164. index: 8
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): bittrex.com
DEBUG:urllib3.connectionpool:https://bittrex.com:443 "GET /api/v1.1/public/getorderbook?market=BTC-LTC&type=both HTTP/1.1" 200 46568
Time: 0.09714889526367188. index: 9

2 个答案:

答案 0 :(得分:1)

请参阅:
Python requests module is very slow on specific machine
Why is Python 3 http.client so much faster than python-requests?
requests: how to disable / bypass proxy

对于某些人来说,这个问题似乎与代理人有关 对您的代码进行的这一更改为我带来了巨大的速度。

import requests
import time
import logging
logging.basicConfig(level=logging.DEBUG)
url = "https://bittrex.com/api/v1.1/public/getorderbook?market=BTC-LTC&type=both"
session = requests.Session()
session.trust_env = False

for i in range(10):
    start = time.time()

    session.get(url)

    end = time.time()

    print('Time: {}. index: {}'.format(end - start, i))

来自requests: how to disable / bypass proxy
作者:@Lukas Graf
“我目前唯一能够完全禁用代理的方法如下:

创建会话
将session.trust_env设置为False
使用该会话创建您的请求“

您的里程可能会有所不同

答案 1 :(得分:1)

我也有同样的问题。第一个请求需要5秒,而其他请求很快。 我尝试更新请求和python,问题仍然存在。然后,我检查服务器的负载和网络连接,没有错。 我尝试了这段代码:

logging.basicConfig()   
logging.getLogger().setLevel(logging.DEBUG)  
requests_log = logging.getLogger("requests.packages.urllib3")  
requests_log.setLevel(logging.DEBUG)  
requests_log.propagate = True  

它显示第一个DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1)持续5秒钟。

我也尝试使用会话而不是获取,没有任何改善。

最后,我将域添加到/etc/hosts,问题已解决。

所以我认为关键是DNS。

相关问题