Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Python requests.post this slow or am I doing something wrong?

I'm a new python programmer and was tasked to do a simple app to make an API call. It's working fine and all but however I am not satisfied with the speed. I have tried it on both slow speed internet connection and faster speed internet connection. On average, the time taken to make the API call ranging from 1.3 to 2.4 sec. I have tested this on 5mbps to 80mbps internet connection and both shown similar result (plus minus 0.5 sec difference).
 
The time taken doesn't look too bad on paper but however, when I checked in the server where the call was made to, the processing time is only around 0.2 - 0.5 seconds, meaning there's 1 - 2 seconds of time lost during the request process.
 
Here's the code that I'm using:

import requests
import json
import time
import uuid
import requests.packages.urllib3

requests.packages.urllib3.disable_warnings() 

#get api call start time
startTime = time.time()

#make api call
r=requests.post(APIUrl, data=payload, headers=headers, verify=False)

#get api call finish time
endTime = time.time()

#calculate time taken
totalTimeTaken = str(float(round((endTime - startTime ),3)))

json_obj = r.json(strict=False)
print "Response: "+str(json_obj['response'])
print "Elapsed: "+str(r.elapsed)
print "Time Taken: "+totalTimeTaken
like image 212
akmalzamri Avatar asked Oct 29 '22 10:10

akmalzamri


1 Answers

Adding @Liping Huang's comment as an answer so it's more visible as most people don't read comments.

Changing the URL from localhost to 127.0.0.1 reduces the overhead by almost 2 seconds to almost nothing.


This means that DNS lookups have been slow for the OP, it's not related to requests.post

like image 100
Motti Avatar answered Nov 15 '22 06:11

Motti