Reputation: 305
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!
Upvotes: 3
Views: 1119
Reputation: 396
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
Upvotes: 2