Python 如何从Flask服务器异步发送多个请求?

Python 如何从Flask服务器异步发送多个请求?,python,asynchronous,python-requests,Python,Asynchronous,Python Requests,所以我正在构建一个FlaskAPI,我有一个GET路由,它基本上向另一个API发送5个请求来收集数据,然后合并并返回 像这样: results = [requests.get('http://localhost:9000/ss/1'), requests.get('http://localhost:9000/ss/2'), requests.get('http://localhost:9000/ss/3'), requests.get('ht

所以我正在构建一个FlaskAPI,我有一个GET路由,它基本上向另一个API发送5个请求来收集数据,然后合并并返回

像这样:

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')]
问题是每个请求大约需要2秒,因此需要10秒才能完成。如何使用不同的线程使所有请求异步,这样总共需要2秒左右?那么,当它们全部加载后,我如何告诉API开始合并它们呢


非常感谢您的帮助

您可以使用
grequests
package(),它最初是基于gevent和请求的

代码如下所示:

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