Flask provides the signal function, which is a message distribution mechanism. Similar to Hooks. Using signal functions can reduce program coupling and decompose complex business models. For example, after updating product data, a signal can be sent. When there is a function that needs to process product data, the signal can be captured for processing. For example, you need to create a product cache, or update the search index, etc.
Define signal
Flask signal function 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 the singles object packaged by Flask:
from flask.singles import Namespace
Send signal
To send a signal, you need to bring the app instance method. The example is as follows:
product_saved.send(app, product=product)
app You can add parameters to be passed later, but they must be in the format of name=value. Using a single variable name is not supported.
Receive signals
To receive signals, you can use the connect_via decorator function:
@product_saved.connect_via(app) def updateCache(app, product): print(product)
Flask has the following core signals:
1.flask.template_rendered
This signal is sent after a template is successfully rendered. The template passed by the signal is an instance of the template, and the context is an environment object which is a dictionary.
Subscription example:
def log_template_renders(sender, template, context, **extra): sender.logger.debug('Rendering template "%s" with context %s', template.name or 'string template', context) from flask import template_rendered template_rendered.connect(log_template_renders, app)
2.flask.request_started
This signal is sent in the request Before starting, and after the request environment is set up. Because the request context is already bound, subscribers can use standard global proxies such as request to operate on requests.
Subscription example:
def log_request(sender, **extra): sender.logger.debug('Request context is set up') from flask import request_started request_started.connect(log_request, app) flask.request_finished
This signal is sent before sending a response to the client. The response passed by the signal is the response to be sent.
Subscription example:
def log_response(sender, response, **extra): sender.logger.debug('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 during the request. It is sent earlier than standard exception handling. In debugging mode, although there is no exception handling, this signal is also sent when an exception occurs. The exception passed by the signal is an exception object.
Subscription example:
def log_exception(sender, exception, **extra): sender.logger.debug('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, regardless of whether an exception is thrown. Currently, the function listening for this signal is called after the general crash handler, but nothing is available.
Subscription example:
def close_db_connection(sender, **extra): session.close()from flask import appcontext_tearing_down request_tearing_down.connect(close_db_connection, app)
In Flask version 0.9, this will also pass an exc keyword argument, if one exists. This parameter is a reference to the exception that caused the crash.
3.flask.appcontext_tearing_down
Send this signal when the application environment crashes. This signal is always sent, even on a crash caused by an exception. Functions listening for this signal will be called after the regular crash handler, but you cannot respond to this signal.
Subscription example:
def close_db_connection(sender, **extra): session.close()from flask import request_tearing_down appcontext_tearing_down.connect(close_db_connection, app)
This will also pass an exc keyword argument, if one exists. This parameter is a reference to the exception that caused the crash.
4.flask.appcontext_pushed
When an application's environment is pushed, the application will send this signal. This signal is typically used in unit tests to temporarily hook information. For example, it can be used to change existing resources in the g object.
Usage example:
from contextlib import contextmanagerfrom flask import appcontext_pushed @contextmanagerdef user_set(app, user): def handler(sender, **kwargs): g.user = user with appcontext_pushed.connected_to(handler, app): yield
Write like this in the test code:
def test_user_me(self): with user_set(app, 'john'): c = app.test_client() resp = c.get('/users/me') assert resp.data == 'username=john' New in version 0.10.
5.appcontext_popped
When an application's context is popped, the application will send this signal. This signal is usually written as appcontext_tearing_down signal.
6.flask.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.
Subscription example:
recorded = []def record(sender, message, category, **extra): recorded.append((message, category)) from flask import message_flashed message_flashed.connect(record, app)
Summary
Signals can make you safe in an instant Subscribe to them. These temporary subscriptions are useful for testing, for example. When using signals, do not let exceptions occur in signal subscribers (receivers), because exceptions will cause program interruption.
For more articles related to the signals signal mechanism in Python’s Flask framework, please pay attention to the PHP Chinese website!