返回响应后运行函数

2024-04-25 18:55:04 发布

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

在Flask中,如何在返回特定路径的响应后运行函数?例如:

# This function takes a long time to run
def long_func():
    ...


 @app.route('/')
 def index():
    ...
    return response

如果我只想在返回索引路由的响应之后运行long_func,我该怎么做呢?在


Tags: to函数run路径appflaskindextime
1条回答
网友
1楼 · 发布于 2024-04-25 18:55:04

一种方法是在调用索引后重定向到调用long函数的路由:

你可以这样做:

@app.route('/')
def index():
    ...
    return redirect(url_for('run_long_function'))
@app.route('/runLongFunc')
def run_long_function():
    long_func()
    return 'done'

相关问题 更多 >