I need to make a request to some server. If the server doesn't respond in 500 ms, I need to send two more (absolutely same) requests and return the first of three that has been finished. I've tried eventlet, grequests, and kind of afraid of making and managing my own threads. I am lost between multiple possible variants. Here is part of the code, but not sure if that would help:
api_responses = []
api_responses.append(grequests.get(URL))
with stopit.ThreadingTimeout(0.5) as first_req_mgr:
grequests.map(api_responses)
while True:
if api_responses[0].status_code == 200:
break
if first_req_mgr.state == first_req_mgr.TIMED_OUT:
#send two more requests and wait in while true loop for any of them to return successful response
EDIT: First request should not be killed after 500 seconds