I am trying to send requests concurrently to a server and then record the average latency using this code:
import Queue
import time
import threading
import urllib2
data = "{"image_1":"abc/xyz.jpg"}"
headers = {.....}
def get_url(q, url):
num = 1
sum = 0
while num <= 200:
start = time.time()
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
end = time.time()
print end - start
num = num + 1
q.put(response.read())
sum = sum + (end - start)
print sum
theurls = ["http://example.com/example"]
q = Queue.Queue()
for u in theurls:
t = threading.Thread(target = get_url, args = (q, u))
t.daemon = True
t.start()
while True:
s = q.get()
print s
This code is working just fine, but now I intend to send more than 1000 requests per second. I came across this answer but I am not sure how do I use grequests
for my case. Some insights will be very helpful.
Thanks