I am working on a Flask application which needs to return HTTP 200 OK under 2 seconds. But my app has to initiate some work after sending HTTP 200 OK.
@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
The problem is respond function takes more than 2 seconds to process data it received. I need it to send an HTTP 200 request first then start it's work.
EDIT
Below is the code for respond function.
import requests
def respond(data):
if data['type'] == "event_callback":
event = data['event']
if event['type'] == 'message':
r_data = requests.post('URL', data = {'key' : 'data')
else:
r_data = {
"error" : 'Type not recognized'
}
return r_data