flask-----信号

安装:

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

python 复制代码
pip install blinker

flask其中有内置的信号

python 复制代码
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:模版渲染完毕后发送,示例如下:

python 复制代码
    from flask import template_rendered      #定义函数写在__init__中
    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之类的标准全局代理访问请求。示例如下:

python 复制代码
    def log_request_started(sender,**extra):
        print('sender:',sender)
        print('extra:',extra)
    request_started.connect(log_request_started,app)

3.flask.request_finished:请求结束时,在响应发送给客户端之前发送,可以传递response,示例代码如下:

python 复制代码
    def log_request_finished(sender,response,*args):
        print( 'response:',response)
    request_finished.connect(log_request_finished,app)

自定义信号

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

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

python 复制代码
#1.定义信号
Myspace = Namespace()
fire_signal = Myspace.signal('fire') #fire为信号名称

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

python 复制代码
def fire_bullet(sender,username):
    print('开始射击')
    print(username)
 
fire_signal.connect(fire_bullet)

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

python 复制代码
#3.发送信号
fire_signal.send(username='xxxxxx')
python 复制代码
#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')
 
相关推荐
n8n2 分钟前
Spring AI 工具调用实战:模型自主决策、@Tool 方法封装与 ReAct 模式
后端
只爱喝胡辣汤6 分钟前
05-JVM 监控与故障排查工具
后端
满怀冰雪17 分钟前
25-迁移学习入门:加载预训练模型并微调
人工智能·python·机器学习·迁移学习·paddle
2601_9622845018 分钟前
安卓APP UI自动化测试:Python + UiAutomator2 + pytest + pytest-html
python·pytest·uiautomator2·ui自动化测试·安卓app
geovindu20 分钟前
CSharp: Task Scheduler
开发语言·后端·c#·.net·.netcore
2601_9620780330 分钟前
构建RESTful APIs:使用Python和Flask
python·flask·web开发·api设计·构建restfulapis
半亩码田31 分钟前
C#转Python第4.5篇:单元测试:pytest vs xUnit/NUnit
python·单元测试·c#
2601_9623008136 分钟前
Python + Requests + Pytest + Excel + Allure:接口自动化测试项目实战
python·excel·pytest·allure·requests
Zane199438 分钟前
两个线程算两遍循环,为什么只快了一点点不是两倍?GIL连环追问
后端·python
重生之小比特39 分钟前
【Java SE】程序逻辑控制
java·开发语言·python