signals signal mechanism in Python's Flask framework

高洛峰
Release: 2017-03-03 13:27:16
Original
1300 people have browsed it

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
Copy after login

Define a signal:

from blinker import Namespace
product_saved = Namespace()
Copy after login

You can also use the singles object packaged by Flask:

from flask.singles import Namespace
Copy after login

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)
Copy after login

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)
Copy after login

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)
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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)
Copy after login

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)
Copy after login

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
Copy after login

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.
Copy after login

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)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!