【问题标题】:First HTTPS request takes much more time than the rest第一个 HTTPS 请求比其他请求花费更多的时间
【发布时间】:2017-08-20 15:14:07
【问题描述】:

我正在制作一个需要进行快速 HTTP API 调用的 Python 应用程序。 我使用 python requests 包在 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

【问题讨论】:

  • 如果没有您的特定代码或要测试的东西,我们怎么知道?
  • @roganjosh 你是对的。已添加。
  • @AlexVelickiy 你能解决这个问题吗?

标签: python http networking https python-requests


【解决方案1】:

我也有同样的问题。第一个请求需要 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。

【讨论】:

    【解决方案2】:

    见:
    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
    使用该会话创建您的请求”

    您的里程可能会有所不同

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      • 2011-06-10
      • 1970-01-01
      相关资源
      最近更新 更多