LoginSignup
1
0

More than 3 years have passed since last update.

Flask で後始末メソッドが呼ばれる順序

Last updated at Posted at 2019-08-16

(この記事は私の blog の http://umezawa.dyndns.info/wordpress/?p=7337 の転載です)

概要

Flask で、後始末メソッドである teardown_appcontext teardown_request after_request が呼ばれる順序。ググってもそのものズバリがヒットしなかったので。

from flask import Flask

app = Flask(__name__)

@app.after_request
def after_request(x):
    print("after_request")
    return x

@app.teardown_request
def teardown_request(x):
    print("teardown_request")

@app.teardown_appcontext
def teardown_appcontext(x):
    print("teardown_appcontext")

@app.route("/")
def view():
    return ""

app.run(debug=True, host="0.0.0.0")

結果

[umezawa@devubuntu:pts/0 ~]$ python3 flask_teardown_order.py
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 702-370-689
after_request
teardown_request
teardown_appcontext
192.168.0.4 - - [16/Aug/2019 20:31:53] "GET / HTTP/1.1" 200 -

環境

  • Ubuntu 18.04 LTS
  • Flask 0.12.2
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0