python - First HTTPS request takes much more time than the rest -


i'm making python app needs make fast http api calls. use python requests package make api calls on https. noticed first request takes more time rest. not due establishing http connection because requests.get() establishes new connection on every call (checked requests logs).

i investigated network wireshark , although found first request takes more time others, difference not big.

i noticed if make first call host1 , rest host2, request host1 takes more time subsequent requests. assures me issue not connected establishing kind of connection host.

i checked ping command , didn't find such difference between first , subsequent ping requests.

is os, requests initial set on first request or else?

my python code:

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"  in range(10):     start = time.time()      requests.get(url)      end = time.time()      print('time: {}. index: {}'.format(end - start, i)) 

output:

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 

see:
python requests module slow on specific machine
why python 3 http.client faster python-requests?
requests: how disable / bypass proxy

for issue seems around proxies.
change code speeded things dramatically me.

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  in range(10):     start = time.time()      session.get(url)      end = time.time()      print('time: {}. index: {}'.format(end - start, i)) 

from requests: how disable / bypass proxy
written @lukas graf
"the way i'm aware of disabling proxies entirely following:

create session
set session.trust_env false
create request using session"

your mileage may differ


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -