0
之前我询问过Make a non-blocking request with requests when running Flask with Gunicorn and Gevent,现在我对相关主题提出了更高级的问题。使用Gunicorn和Gevent运行Flask时请求异步Web请求
我的Flask应用程序将收到一个请求,进行一些处理,然后向两个不同的慢速外部端点发出两个不同的请求,每个端点需要2秒钟响应。我的烧瓶代码如下:
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
我跑我的
gunicorn server:app -k gevent -w 4
gunicorn有了这个代码,对于发送到这个网络服务的每一个要求,我需要等待4秒响应(它首先发送'慢api',然后'慢api b')。我如何修改示例以便请求发送到'slow api a'和'slow api b'可以同时发送,因此我可以在2秒内从4秒钟内收到此Web服务的响应?
Python版本? 3.5? 3.6? – Grimmy
使用[grequests](https://github.com/kennethreitz/grequests)而不是请求 –
python版本是2.7,将调查grequests。 – JLTChiu