Skip to content

Instantly share code, notes, and snippets.

@guyskk
Last active January 12, 2018 02:13
Show Gist options
  • Select an option

  • Save guyskk/e7bb6d284c1992205f4f33366b598246 to your computer and use it in GitHub Desktop.

Select an option

Save guyskk/e7bb6d284c1992205f4f33366b598246 to your computer and use it in GitHub Desktop.
理解Flask上下文环境
import flask
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "hello"
@app.before_request
def before_request():
print("before_request")
@app.teardown_request
def teardown_request(ex):
print("teardown_request")
@app.teardown_appcontext
def teardown_appcontext(ex):
print("teardown_appcontext")
@flask.appcontext_pushed.connect_via(app)
def appcontext_pushed(sender, *args, **kwargs):
print("appcontext_pushed")
@flask.appcontext_popped.connect_via(app)
def appcontext_popped(sender, *args, **kwargs):
print("appcontext_popped")
@flask.appcontext_tearing_down.connect_via(app)
def appcontext_tearing_down(sender, *args, **kwargs):
print("appcontext_tearing_down")
@flask.request_tearing_down.connect_via(app)
def request_tearing_down(sender, *args, **kwargs):
print("request_tearing_down")
@flask.request_finished.connect_via(app)
def request_finished(sender, *args, **kwargs):
print("request_finished")
@flask.request_started.connect_via(app)
def request_started(sender, *args, **kwargs):
print("request_started")
if __name__ == '__main__':
with app.app_context():
print("-" * 60)
with app.test_client() as c:
c.get("/")
print("-" * 40)
with app.test_client() as c:
c.get("/")
print("-" * 60)
print("-" * 40)
with app.test_client() as c:
c.get("/")
print("-" * 40)
with app.test_client() as c:
c.get("/")
app.run()
appcontext_pushed
------------------------------------------------------------
request_started
before_request
request_finished
teardown_request
request_tearing_down
----------------------------------------
request_started
before_request
request_finished
teardown_request
request_tearing_down
------------------------------------------------------------
teardown_appcontext
appcontext_tearing_down
appcontext_popped
----------------------------------------
appcontext_pushed
request_started
before_request
request_finished
teardown_request
request_tearing_down
teardown_appcontext
appcontext_tearing_down
appcontext_popped
----------------------------------------
appcontext_pushed
request_started
before_request
request_finished
teardown_request
request_tearing_down
teardown_appcontext
appcontext_tearing_down
appcontext_popped
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
appcontext_pushed
request_started
before_request
request_finished
teardown_request
request_tearing_down
teardown_appcontext
appcontext_tearing_down
appcontext_popped
127.0.0.1 - - [11/Oct/2016 16:32:55] "GET / HTTP/1.1" 200 -
@guyskk
Copy link
Copy Markdown
Author

guyskk commented Oct 11, 2016

请求上下文与应用上下文是一一对应的。
在手动推入应用上下文的情况下,一个应用上下文对应多个请求上下文,
这是异常情况,一般会在测试的时候出现,需要避免这样的写法。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment