Flask provides signals (Signals) feature is a message distribution mechanism. Similar to hooks (Hooks). The use of signals can reduce program coupling and decompose complex business models. For example, after updating the product data, you can send a signal. When there is a need for product data processing functions, you can capture the signal for processing. For example, to create a product cache, or to update a search index.
Defining Signals
The Flask signaling feature uses the Blinker module, so you need to install the Blinker module first.
pip install blinker
Define a signal.
from blinker import Namespace product_saved = Namespace()
You can also use Flask's wrapped singles object.
from import Namespace
Send Signal
To send a signal, you need to take the app instance method with you, as shown in the following example:
product_saved.send(app, product=product)
app can be followed by parameters to be passed, but they must be in the format name=value; the use of individual variable names is not supported.
receive a signal
Receiving signals can be done using the connect_via decorator function.
@product_saved.connect_via(app) def updateCache(app, product): print(product)
There are the following core signals in Flask.
.template_rendered
This signal is sent after a template has been successfully rendered. The signal passes template is an instance of the template and context is a dictionary of environment objects.
Example of a subscription.
def log_template_renders(sender, template, context, **extra): ('Rendering template "%s" with context %s', or 'string template', context) from flask import template_rendered template_rendered.connect(log_template_renders, app)
.request_started
This signal is sent before the request is started and after the request environment has been set up. Because the request environment is already bound, subscribers can use standard global proxies such as request to manipulate the request.
Example of a subscription.
def log_request(sender, **extra): ('Request context is set up') from flask import request_started request_started.connect(log_request, app) flask.request_finished
This signal is sent before the response is sent to the client. The response that is signaled is the response that will be sent.
Example of a subscription.
def log_response(sender, response, **extra): ('Request context is about to close down. ' 'Response: %s', response) from flask import request_finished request_finished.connect(log_response, app) flask.got_request_exception
This signal is sent when an exception occurs while the request is in progress. It is sent before standard exception handling. In debug mode, this signal is also sent when an exception occurs, even though there is no exception handling. The exception that is signaled is the exception object.
Example of a subscription.
def log_exception(sender, exception, **extra): ('Got exception during processing: %s', exception) from flask import got_request_exception got_request_exception.connect(log_exception, app) flask.request_tearing_down
This signal is sent when the request crashes, whether or not an exception is thrown. Currently, the function that listens for this signal is called after the general crash handler, but there is nothing available.
Example of a subscription.
def close_db_connection(sender, **extra): ()from flask import appcontext_tearing_down request_tearing_down.connect(close_db_connection, app)
In Flask version 0.9, this also passes an exc keyword argument, if one exists. This parameter is a reference to the exception that threw the crash.
.appcontext_tearing_down
This signal is sent when the application environment crashes. This signal is always sent, even if the crash was triggered by an exception. The function that listens for this signal is called after the regular crash handler, but you can't return this signal.
Example of a subscription.
def close_db_connection(sender, **extra): ()from flask import request_tearing_down appcontext_tearing_down.connect(close_db_connection, app)
This also passes an exc keyword argument, if one exists. This parameter is a reference to the exception that threw the crash.
.appcontext_pushed
This signal is sent by an application when its environment is pressed in. This signal is often used to temporarily hook up information in unit tests. It can be used, for example, to change existing resources in a g-object.
Usage examples.
from contextlib import contextmanagerfrom flask import appcontext_pushed @contextmanagerdef user_set(app, user): def handler(sender, **kwargs): = user with appcontext_pushed.connected_to(handler, app): yield
In the test code, write this.
def test_user_me(self): with user_set(app, 'john'): c = app.test_client() resp = ('/users/me') assert == 'username=john' New in version 0.10.
5.appcontext_popped
An app sends this signal when its environment is ejected. This signal is usually written as the appcontext_tearing_down signal.
.message_flashed
This signal is emitted when the application flashes a message. the message` parameter is the message content and the `category parameter is the message category.
Example of a subscription.
recorded = []def record(sender, message, category, **extra): ((message, category)) from flask import message_flashed message_flashed.connect(record, app)
wrap-up
Signals allow you to safely subscribe to them at a moment's notice. These temporary subscriptions are useful for testing, for example. When using signals, do not allow exceptions to occur to the subscriber (receiver) of the signal, because exceptions can cause program interruptions.