3

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!

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.