flask信号使用


flask信号:

安装:

flask中的信号使用的是一个第三方插件,叫做blinker。通过pip list看一下,如果没有安装,通过以下命令即可安装blinker

pip install blinker

 自定义信号:

分为三步:1.创建信号  2.监听信号  3.发送信号

定义信号:定义信号需要使用到blinker这个包的Namespace类来创建一个命名空间。比如定义一个在访问了某个视图函数的时候的信号。示例代码如下:

#1.定义信号
Myspace = Namespace()
fire_signal = Myspace.signal('fire') #fire为信号名称

监听信号:监听信号使用singal对象的connect方法,在这个方法中需要传递一个函数,用来接收以后监听到这个信号该做的事情。示例代码如下:

def fire_bullet(sender,username):
    print('开始射击')
    print(username)

fire_signal.connect(fire_bullet)

发送信号:发送信号使用singal对象的send方法,这个方法可以传递一些其他参数过去。示例代码如下:

#3.发送信号
fire_signal.send(username='xxxxxx')
#encoding:utf-8
# __author__ = 'donghao'
# __time__ = 2019/1/3 19:22
from blinker import Namespace

#Namespace命名空间

#1.定义信号
Myspace = Namespace()
fire_signal = Myspace.signal('fire') #fire为信号名称

# 2.监听信号
# fire_bullet 接受参数:发送者sender
def fire_bullet(sender,username):
    print(sender)
    print('开始射击')
    print(username)


fire_signal.connect(fire_bullet)

#3.发送信号
fire_signal.send('xxx',username='donghao')

内置信号:

template_rendered = _signals.signal('template-rendered')#模板渲染完成的信号
before_render_template = _signals.signal('before-render-template') #模板渲染前的信号
request_started = _signals.signal('request-started') #模板开始渲染
request_finished = _signals.signal('request-finished') #模板渲染完成
request_tearing_down = _signals.signal('request-tearing-down') #request对象被销毁的信号
got_request_exception = _signals.signal('got-request-exception') #视图函数发生异常的信号
appcontext_tearing_down = _signals.signal('appcontext-tearing-down') #app上下文被摧毁的信号
appcontext_pushed = _signals.signal('appcontext-pushed')#app上下文被推入栈上的信号
appcontext_popped = _signals.signal('appcontext-popped')#app上下文被推出栈上的信号
message_flashed = _signals.signal('message-flashed')#flask的flush方法的信号

 例如:

  1. flask.template_rendered:模版渲染完毕后发送,示例如下:

        from flask import template_rendered
        def log_template_renders(sender,template,context,*args):
            print( 'sender:',sender)
            print ('template:',template)
            print ('context:',context)
    
        template_rendered.connect(log_template_renders,app)
    
  2. flask.request_started:请求开始之前,在到达视图函数之前发送,订阅者可以调用request之类的标准全局代理访问请求。示例如下:

        def log_request_started(sender,**extra):
            print('sender:',sender)
            print('extra:',extra)
        request_started.connect(log_request_started,app)
    
  3. flask.request_finished:请求结束时,在响应发送给客户端之前发送,可以传递response,示例代码如下:

        def log_request_finished(sender,response,*args):
            print( 'response:',response)
        request_finished.connect(log_request_finished,app)
    
  4. flask.got_request_exception:在请求过程中抛出异常时发送,异常本身会通过exception传递到订阅的函数。示例代码如下:

        def log_exception_finished(sender,exception,*args):
            print ('sender:',sender)
            print (type(exception))
        got_request_exception.connect(log_exception_finished,app)
    
  5. flask.request_tearing_down:请求被销毁的时候发送,即使在请求过程中发生异常,也会发送,示例代码如下:

        def log_request_tearing_down(sender,**kwargs):
            print ('coming...')
        request_tearing_down.connect(log_request_tearing_down,app)
    
  6. flask.appcontext_tearing_down:在应用上下文销毁的时候发送,它总是会被调用,即使发生异常。示例代码如下:

        def log_appcontext_tearing_down(sender,**kwargs):
            print ('coming...')
        appcontext_tearing_down.connect(log_appcontext_tearing_down,app)
    
原文地址:https://www.cnblogs.com/donghaoblogs/p/10389697.html