-
-
Save Asoul/2cd17fae850e959c4d17c15e3daf6e10 to your computer and use it in GitHub Desktop.
理解Flask上下文环境
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 - |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment