0

In Flask, how could I run a function after returning a response for a particular route? For example:

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


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

If I wanted to run long_func after returning the response for only the index route, how could I do that?

Jasonca1
  • 4,848
  • 6
  • 25
  • 42

1 Answers1

0

one way to do it is to redirect to a route calling the long function after calling your index :

you can do something like this :

@app.route('/')
def index():
    ...
    return redirect(url_for('run_long_function'))
@app.route('/runLongFunc')
def run_long_function():
    long_func()
    return 'done'
Espoir Murhabazi
  • 5,973
  • 5
  • 42
  • 73