【发布时间】:2018-06-09 14:01:18
【问题描述】:
在 Flask 中,如何在返回特定路由的响应后运行函数?例如:
# This function takes a long time to run
def long_func():
...
@app.route('/')
def index():
...
return response
如果我想在只返回索引路由的响应后运行 long_func,我该怎么做?
【问题讨论】:
在 Flask 中,如何在返回特定路由的响应后运行函数?例如:
# This function takes a long time to run
def long_func():
...
@app.route('/')
def index():
...
return response
如果我想在只返回索引路由的响应后运行 long_func,我该怎么做?
【问题讨论】:
一种方法是在调用索引后重定向到调用 long 函数的路由:
你可以这样做:
@app.route('/')
def index():
...
return redirect(url_for('run_long_function'))
@app.route('/runLongFunc')
def run_long_function():
long_func()
return 'done'
【讨论】: