python - 第一个 HTTPS 请求比其他请求花费更多时间

标签 python http networking https python-requests

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

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

我还注意到,即使我第一次调用主机 1,其余所有调用都调用主机 2,对主机 1 的请求比所有后续请求花费的时间要多得多。这让我确信问题与与主机建立某种连接无关。

我检查了 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

最佳答案

我也有同样的问题。第一个请求需要5秒,其他的很快。 我尝试更新 requests 和 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)持续了5s。

我也尝试过使用session代替get,没有任何改善。

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

所以我认为关键是 DNS。

关于python - 第一个 HTTPS 请求比其他请求花费更多时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45783655/

相关文章:

java - 适用于Java的NMap包装器

rest - 在 DELETE 之后返回集合的剩余部分是否考虑不好的做法

c++ - 错误 : swscale headers not found! 安装 FFmpeg 或 Libav

python - 使用 Google App Engine (Python) 返回数据(json、xml 等)的 jQuery Post

python - 用于执行 Python 程序的 Bash 脚本

java - 来自 java 的 HTTP POST 作为 GET 到达服务器

http认证缓存

c - lwip ip_addr - 存储大小

python - 来自两个文本文件的行的组合

python - tf.data.Dataset.map() 的输出张量形状有些奇怪