在flas中返回http200ok后执行函数

2024-06-02 05:35:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个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}$

Tags: tokenjsonapp应用程序flaskdataifresponse