python flask-处理请求后运行脚本 [英] python flask - run script after processing the request
问题描述
我是烧瓶的新手...
I'm a total newbie at flask...
在用flask处理后请求后,我想运行一个长脚本(大约需要30秒).我找到了 @ app.after_request
选项,但问题是flask仅在处理完请求后脚本后才返回响应.这是我的代码-
I would like to run a long script (which takes about 30 second) after processing a post request with flask.
I've found the @app.after_request
option, but the problem is flask only returns the response after processing the after request script.
here is my code -
#!/usr/bin/python
from flask import Flask, request
import json
import subprocess
import time
import commands
from flask import g
app = Flask(__name__)
@app.route('/payload',methods=['POST'])
def processWebhook():
return 'OK', 200
@app.after_request
def runScript(response):
time.sleep(30) #this is where my script will run
return response
if __name__ == '__main__':
app.run(port=4995,host='0.0.0.0')
如何使flask返回请求并在请求后运行脚本?
how can I make flask to return the response and run the script after the request?
任何帮助将不胜感激.
推荐答案
您可以使用 APScheduler 每小时运行一次代码后台,并在发布必要的数据后立即返回响应.
You can use APScheduler to run your code background every hour and return the response immediately after posting necessary data.
您可以在该链接中查看演示.
You can see the demo in that link.
这篇关于python flask-处理请求后运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!