I asked Make a non-blocking request with requests when running Flask with Gunicorn and Gevent before, and now I have a more advanced question on related topics.
My Flask application will receive a request, do some processing, and then make two different requests to two different slow external endpoints that each takes 2 seconds to respond. My flask code looks as follows:
import requests
@app.route('/do', methods = ['POST'])
def do():
resulta = requests.get('slow api a') // takes 2 seconds to response
resultb = requests.get('slow api b') // takes 2 seconds to response
return resulta.content + resultb.content
And I run my gunicorn with
gunicorn server:app -k gevent -w 4
With this code, for every request that send to this web service, I need to wait 4 seconds to response (It send to 'slow api a' first, then do 'slow api b'). How can I modify the example so that the the request send to 'slow api a' and 'slow api b' can be sent at the same time, so I can get a response from this web service in 2 second instead of 4 seconds?