2017-08-20 13 views
2

私は高速なHTTP API呼び出しを行う必要のあるPythonアプリケーションを作っています。 私はpython要求パッケージを使ってHTTPS上でAPI呼び出しを行います。 私は、最初の要求は常に残りの部分よりも多くの時間がかかることに気付きました。 これは、requests.get()がすべての呼び出し(ログでチェックされた要求)で新しい接続を確立するため、HTTP接続を確立するためではありません。最初のHTTPSリクエストに残りの時間よりも多くの時間がかかります

Wiresharkでネットワークを調査しましたが、最初のリクエストには他のリクエストよりも時間がかかることがわかりましたが、その違いは大きくありません。

私はhost1への最初の呼び出しと残りのすべてをhost2に行っても、host1への要求には後続のすべての要求よりも多くの時間がかかることに気付きました。これは、問題がホストと何らかの種類の接続を確立することに関連していないことを私に保証する。

私はpingコマンドで確認して、最初と次のping要求の間にそのような違いは見つかりませんでした。

OSとは関係がありますか?最初のリクエスト時に初期設定をリクエストするなどですか?

私の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 
+0

特定のコードや何かをテストすることなく、どうやって知ることができますか? – roganjosh

+0

@roganjoshあなたが正しいです。追加されました。 –

+0

@AlexVelickiyさんはこの問題を解決できましたか? – teng

答えて

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

は思えますプロキシの周りにいる。
コードへのこの変更は、私にとって劇的に高速化しました。 @Lukasグラーフ
「私は現在、完全にプロキシを無効に従っているための承知している唯一の方法によって書かれた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)) 

にsession.trust_envセッション
セットを作成False
そのセッションを使用してリクエストを作成してください。 "

マイルが異なる場合があります

関連する問題