From the blog http://www.cnblogs.com/caochao-/p/8997831.html
Falsk-Signal
The signals in the Flask framework are based on blinker, which mainly allows developers to customize some user behaviors during the flask request process.
request_started = _signals.signal('request-started') # Execute before the request arrives
request_finished = _signals.signal('request-finished') # Execute after the request is over
before_render_template = _signals.signal('before-render-template') # Execute before template rendering
template_rendered = _signals.signal('template-rendered') # Execute after template rendering
got_request_exception = _signals.signal('got-request-exception') # Execute when an exception occurs in the request execution
request_tearing_down = _signals.signal('request-tearing-down') # Automatically execute after the request is executed (whether successful or not)
appcontext_tearing_down = _signals.signal('appcontext-tearing-down')# Automatically execute after the request context is executed (whether successful or not)
appcontext_pushed = _signals.signal('appcontext-pushed') # Executed when requesting context push
appcontext_popped = _signals.signal('appcontext-popped') # Execute when the context pop is requested
message_flashed = _signals.signal('message-flashed') # Automatically trigger when flask is called to add data to it
execution order
appcontext_pushed = _signals.signal('appcontext-pushed')
request_started = _signals.signal('request-started')
If there is a render:
before_render_template = _signals.signal('before-render-template')
template_rendered = _signals.signal('template-rendered')
request_finished = _signals.signal('request-finished')
If the view function has an exception:
got_request_exception = _signals.signal('got-request-exception')
request_tearing_down = _signals.signal('request-tearing-down')
appcontext_tearing_down = _signals.signal('appcontext-tearing-down')
appcontext_popped = _signals.signal('appcontext-popped')
If using signals:
message_flashed = _signals.signal('message-flashed')
use:
from flask import Flask,signals,render_template,flash
app = Flask(__name__)
def func1(*args,**kwargs):
print('Trigger signal: request_started')
def func2(*args,**kwargs):
print('Trigger signal: appcontext_pushed')
signals.request_started.connect(func1)
signals.appcontext_pushed.connect(func2)
@app.route('/login')
def login():
return "LOGIN"
if __name__ == '__main__':
app.run()
The difference between signal and before_request()
Signal: Real-time monitoring of data operations (logging)
Add additional operations on the basis of the original request processing (signals are executed before befors_request)
befors_requets: can control whether the request can continue to go down (can have a return value)