Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to SEND multiple requests ASYNC from Flask server?

So I'm building a Flask API, and I have a GET route that basically sends 5 requests to another API to gather data that I'm then merging and returning.

Like so:

results = [requests.get('http://localhost:9000/ss/1'),
            requests.get('http://localhost:9000/ss/2'),
        requests.get('http://localhost:9000/ss/3'),
       requests.get('http://localhost:9000/ss/4'),
            requests.get('http://localhost:9000/ss/5')]

The problem is each request takes about 2 seconds, so it takes 10 seconds for this to go down. How can I make all the requests async using different threads so it will take around 2 seconds overall? And then how do I tell the API to start merging them when theyre all loaded?

Any help is GREATLY appreciated!

like image 875
Aric Liesenfelt Avatar asked Dec 12 '25 03:12

Aric Liesenfelt


1 Answers

You can use grequests package( https://pypi.python.org/pypi/grequests) that is originally based on gevent I think and requests.

Code is as simple as:

urls = [url1,url2,...]
#prepare the shells 
shells = (grequests.get(u) for u in urls)
#start all requests at the same time
responses = grequests.map(shells) #will output a list of responses that you can access
like image 132
Fnaxiom Avatar answered Dec 14 '25 11:12

Fnaxiom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!