Help us understand the problem. What is going on with this article?

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

(この記事は私の 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
umezawatakeshi
AVX-512 が実装されたパフォーマンスデスクトップ向けプロセッサを待ち続けて早幾年月
http://umezawa.dyndns.info/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした