gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 16:32:07 30 4
gpt4 key购买 nike

我正在制作一个需要进行快速 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

最佳答案

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

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com