【问题标题】:Run function after response has been returned [duplicate]返回响应后运行函数[重复]
【发布时间】: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,我该怎么做?

【问题讨论】:

    标签: python flask callback


    【解决方案1】:

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

    你可以这样做:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2014-06-07
      • 1970-01-01
      • 2018-01-14
      相关资源
      最近更新 更多