我正在开发一个Flask应用程序,它需要在2秒内返回http200ok。但是我的应用程序必须在发送http200ok之后启动一些工作。在
@app.route('/events-slack', methods=['GET', 'POST'])
def events():
if(rq.is_json):
data = rq.get_json()
if(data['token']==token):
respond(data) #this function take more than 2 seconds
r_data = {
'OK' : True
}
else:
r_data = {
"error" : 'Token mismatch'
}
else:
r_data = {
"error" : 'Request was not JSON'
}
response = app.response_class(
status=200,
mimetype='application/json',
response=json.dumps(r_data)
)
return response
问题是响应函数处理它接收到的数据需要2秒以上。我需要它先发送一个HTTP200请求,然后开始工作。在
编辑
下面是响应函数的代码。在
^{pr2}$
你需要把重担卸到后台,这样你就不必等待回应了。这通常是使用像芹菜这样的任务队列来完成的。在
http://www.celeryproject.org
你也可以考虑一些轻量级的替代品,比如休伊。在
https://github.com/coleifer/huey/blob/master/README.rst
相关问题 更多 >
编程相关推荐