【发布时间】: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