flask二十: singal
一.信号介绍
Flask框架中的信号基于blinker,其主要就是让开发者可是在flask请求过程中定制一些用户行为。
简单来说就是flask在列表里面,预留了几个空列表,在里面存东西。
信号通过发送通知来帮助你解耦应用。
简言之,信号允许某个发送者通知接收者有事情发生了;
二.内置信号
安装:
pip3 install blinker
2. request_started = _signals.signal('request-started') # 请求到来前执行 5. request_finished = _signals.signal('request-finished') # 请求结束后执行 3. before_render_template = _signals.signal('before-render-template') # 模板渲染前执行 4. template_rendered = _signals.signal('template-rendered') # 模板渲染后执行 发生在2/3/4/5或不执行 got_request_exception = _signals.signal('got-request-exception') # 请求执行出现异常时执行 6. request_tearing_down = _signals.signal('request-tearing-down') # 请求执行完毕后自动执行(无论成功与否) 7. appcontext_tearing_down = _signals.signal('appcontext-tearing-down')# 请求上下文执行完毕后自动执行(无论成功与否) 1. appcontext_pushed = _signals.signal('appcontext-pushed') # 请求app上下文push时执行 8. appcontext_popped = _signals.signal('appcontext-popped') # 请求上下文pop时执行 message_flashed = _signals.signal('message-flashed') # 调用flask在其中添加数据时,自动触发
三.源码
request-started、request-finished源码

class Flask(_PackageBoundObject): def full_dispatch_request(self): self.try_trigger_before_first_request_functions() try: # ############### 触发request_started 信号 ############### request_started.send(self) rv = self.preprocess_request() if rv is None: rv = self.dispatch_request() except Exception as e: rv = self.handle_user_exception(e) response = self.make_response(rv) response = self.process_response(response) # ############### request_finished 信号 ############### request_finished.send(self, response=response) return response def wsgi_app(self, environ, start_response): ctx = self.request_context(environ) ctx.push() error = None try: try: response = self.full_dispatch_request() except Exception as e: error = e response = self.make_response(self.handle_exception(e)) return response(environ, start_response) finally: if self.should_ignore_error(error): error = None ctx.auto_pop(error) request_started
before_render_template、template_rendered源码

def render_template(template_name_or_list, **context): """Renders a template from the template folder with the given context. :param template_name_or_list: the name of the template to be rendered, or an iterable with template names the first one existing will be rendered :param context: the variables that should be available in the context of the template. """ ctx = _app_ctx_stack.top ctx.app.update_template_context(context) return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list), context, ctx.app) def _render(template, context, app): """Renders the template and fires the signal""" # ############### before_render_template 信号 ############### before_render_template.send(app, template=template, context=context) rv = template.render(context) # ############### template_rendered 信号 ############### template_rendered.send(app, template=template, context=context) return rv before_render_template
got_request_exception源码

class Flask(_PackageBoundObject): def handle_exception(self, e): exc_type, exc_value, tb = sys.exc_info() # ############### got_request_exception 信号 ############### got_request_exception.send(self, exception=e) handler = self._find_error_handler(InternalServerError()) if self.propagate_exceptions: # if we want to repropagate the exception, we can attempt to # raise it with the whole traceback in case we can do that # (the function was actually called from the except part) # otherwise, we just raise the error again if exc_value is e: reraise(exc_type, exc_value, tb) else: raise e self.log_exception((exc_type, exc_value, tb)) if handler is None: return InternalServerError() return handler(e) def wsgi_app(self, environ, start_response): ctx = self.request_context(environ) ctx.push() error = None try: try: response = self.full_dispatch_request() except Exception as e: error = e # 这里这里这里这里这里这里这里这里这里这里这里这里 # response = self.make_response(self.handle_exception(e)) return response(environ, start_response) finally: if self.should_ignore_error(error): error = None ctx.auto_pop(error) got_request_exception
request_tearing_down、appcontext_pushed、appcontext_popped源码

def flash(message, category='message'): """Flashes a message to the next request. In order to remove the flashed message from the session and to display it to the user, the template has to call :func:`get_flashed_messages`. .. versionchanged:: 0.3 `category` parameter added. :param message: the message to be flashed. :param category: the category for the message. The following values are recommended: ``'message'`` for any kind of message, ``'error'`` for errors, ``'info'`` for information messages and ``'warning'`` for warnings. However any kind of string can be used as category. """ # Original implementation: # # session.setdefault('_flashes', []).append((category, message)) # # This assumed that changes made to mutable structures in the session are # are always in sync with the session object, which is not true for session # implementations that use external storage for keeping their keys/values. flashes = session.get('_flashes', []) flashes.append((category, message)) session['_flashes'] = flashes # ############### 触发 message_flashed 信号 ############### message_flashed.send(current_app._get_current_object(), message=message, category=category) message_flashed
message_flashed源码

def flash(message, category='message'): """Flashes a message to the next request. In order to remove the flashed message from the session and to display it to the user, the template has to call :func:`get_flashed_messages`. .. versionchanged:: 0.3 `category` parameter added. :param message: the message to be flashed. :param category: the category for the message. The following values are recommended: ``'message'`` for any kind of message, ``'error'`` for errors, ``'info'`` for information messages and ``'warning'`` for warnings. However any kind of string can be used as category. """ # Original implementation: # # session.setdefault('_flashes', []).append((category, message)) # # This assumed that changes made to mutable structures in the session are # are always in sync with the session object, which is not true for session # implementations that use external storage for keeping their keys/values. flashes = session.get('_flashes', []) flashes.append((category, message)) session['_flashes'] = flashes # ############### 触发 message_flashed 信号 ############### message_flashed.send(current_app._get_current_object(), message=message, category=category) message_flashed
四.自定义信号
第一步:创建信号
第二步:将函数注册到信号中: 添加到这个列表
第三步: 发送信号
第四步:运行
具体实现:可参考flask源码,写一个自定义信号
from flask import Flask,flash from flask.signals import _signals app = Flask(__name__) xinhao = _signals.signal("xinhao")#创建信号 #定义函数 def wahaha(*args,**kwargs): print("111",args,kwargs) def sww(*args,**kwargs): print("222",args,kwargs) # 将函数注册到信号中,添加到这个列表 xinhao.connect(wahaha) xinhao.connect(sww) @app.route("/zzz") def zzz(): xinhao.send(sender='xxx',a1=123,a2=456) #触发这个信号,执行注册到这个信号列表中的所有函数,此处的参数个数需要与定义的函数中的参数一致 return "发送信号成功" if __name__ == '__main__': app.run(debug=True) #打印结果 # 111 (None,) {'sender': 'xxx', 'a1': 123, 'a2': 456} # 222 (None,) {'sender': 'xxx', 'a1': 123, 'a2': 456}
posted on 2020-06-28 15:03 myworldworld 阅读(257) 评论(0) 收藏 举报