I need to implement a script to send HTTP requests as quickly as possible. I have tried eventlet + requests. The code as following shown, note that I have set the timeout value to 1 second.
import eventlet
eventlet.monkey_patch(all=False, socket=True)
import requests
params = {
'token': '???',
'data': '???'
}
def request_s(session, *args, **kwargs):
try:
return session.request(*args, **kwargs)
except:
pass # ignore first
finally:
pass
def send():
pile = eventlet.GreenPile(30)
s = requests.Session()
for i in range(100):
pile.spawn(request_s, s, 'https://api.???.com/', params=params, timeout=1)
# Fetch response
for response in pile:
if response:
print response.elapsed, response.text
if __name__ == '__main__':
send()
But I got a strange issue, that is the elapsed time of some requests will exceeds 1 second. The biggest one is about 11 seconds. why?
In this case, eventlet + requests only 4 ~ 6 times faster than the serial model. Is there any better approach to make this process faster?