0
votes

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?

1
python version? 3.5? 3.6?Grimmy
Use grequests instead of requestsMaurice Meyer
python version is 2.7, will look into grequests.JLTChiu

1 Answers

0
votes

You should use multi processing or threading.

import multiprocessing
import time

def testa():
    time.sleep(3)
    return 'a'

def testb():
    time.sleep(3)
    return 'b'

if __name__ == '__main__':
    jobs = []
    for j in [testa, testb]:
        p = multiprocessing.Process(target=j)
        jobs.append(p)
        p.start()
    for k in jobs:
        k.join()

[root@node01 opt]# time python r.py

real 0m3.028s user 0m0.021s sys 0m0.005s [root@node01 opt]