信号(源码)

信号,是在flask框架中为我们预留的钩子,让我们可以进行一些自定义操作。

pip3 install blinker

根据flask项目的请求流程来进行设置扩展点

1.中间件

from flask import Flask,render_template

app = Flask(__name__)

@app.route('/index')
def index():
return render_template('index.html') @app.route('/order')
def order():
return render_template('order.html') class MyMiddleware(object):
def __init__(self,old_app):
self.wsgi_app = old_app.wsgi_app def __call__(self, *args, **kwargs):
print('123')
result = self.wsgi_app(*args, **kwargs)
print('456')
return result app.wsgi_app = MyMiddleware(app) if __name__ == '__main__':
app.run()

2.当app_ctx被push到local中栈之后,会触发appcontext_pushed信号,之前注册在这个信号中的方法,就会被执行。

from flask import Flask,render_template
from flask import signals app = Flask(__name__) @signals.appcontext_pushed.connect
def f1(arg):
print('appcontext_pushed信号f1被触发',arg) @signals.appcontext_pushed.connect
def f2(arg):
print('appcontext_pushed信号f2被触发',arg) @app.route('/index')
def index():
return render_template('index.html') @app.route('/order')
def order():
return render_template('order.html') if __name__ == '__main__':
app.run()
# app.__call__

3.执行before_first_request扩展

from flask import Flask,render_template

app = Flask(__name__)

@app.before_first_request
def f2():
print('before_first_requestf2被触发') @app.route('/index')
def index():
return render_template('index.html') @app.route('/order')
def order():
return render_template('order.html') if __name__ == '__main__':
app.run()

4.request_started信号

from flask import Flask,render_template
from flask import signals
app = Flask(__name__) @signals.request_started.connect
def f3(arg):
print('request_started信号被触发',arg) @app.route('/index')
def index():
return render_template('index.html') @app.route('/order')
def order():
return render_template('order.html') if __name__ == '__main__':
app.run()

5.url_value_processor

from flask import Flask,render_template,g
from flask import signals
app = Flask(__name__) @app.url_value_preprocessor
def f5(endpoint,args):
print('f5') @app.route('/index/')
def index():
print('index')
return render_template('index.html') @app.route('/order')
def order():
print('order')
return render_template('order.html') if __name__ == '__main__':
app.run()

6.before_reuqest

from flask import Flask,render_template,g
from flask import signals
app = Flask(__name__) @app.before_request
def f6():
g.xx = 123
print('f6') @app.route('/index/')
def index():
print('index')
return render_template('index.html') @app.route('/order')
def order():
print('order')
return render_template('order.html') if __name__ == '__main__':
app.run()

视图函数

7.before_render_template / rendered_template

from flask import Flask,render_template,g
from flask import signals
app = Flask(__name__) @signals.before_render_template.connect
def f7(app, template, context):
print('f7') @signals.template_rendered.connect
def f8(app, template, context):
print('f8') @app.route('/index/')
def index():
return render_template('index.html') @app.route('/order')
def order():
print('order')
return render_template('order.html') if __name__ == '__main__':
app.run()

8.after_request

from flask import Flask,render_template,g
from flask import signals
app = Flask(__name__) @app.after_request
def f9(response):
print('f9')
return response @app.route('/index/')
def index():
return render_template('index.html') @app.route('/order')
def order():
print('order')
return render_template('order.html') if __name__ == '__main__':
app.run()

9.request_finished

from flask import Flask,render_template,g
from flask import signals
app = Flask(__name__) @signals.request_finished.connect
def f10(app,response):
print('f10') @app.route('/index/')
def index():
return render_template('index.html') @app.route('/order')
def order():
print('order')
return render_template('order.html') if __name__ == '__main__':
app.run()

10.got_request_exception

from flask import Flask,render_template,g
from flask import signals
app = Flask(__name__) @app.before_first_request
def test():
int('asdf') @signals.got_request_exception.connect
def f11(app,exception):
print('f11') @app.route('/index/')
def index():
return render_template('index.html') @app.route('/order')
def order():
print('order')
return render_template('order.html') if __name__ == '__main__':
app.run()

11.teardown_request

from flask import Flask,render_template,g
from flask import signals
app = Flask(__name__) @app.teardown_request
def f12(exc):
print('f12') @app.route('/index/')
def index():
return render_template('index.html') @app.route('/order')
def order():
print('order')
return render_template('order.html') if __name__ == '__main__':
app.run()

12.request_tearing_down

from flask import Flask,render_template,g
from flask import signals
app = Flask(__name__) @signals.request_tearing_down.connect
def f13(app,exc):
print('f13') @app.route('/index/')
def index():
return render_template('index.html') @app.route('/order')
def order():
print('order')
return render_template('order.html') if __name__ == '__main__':
app.run()

13.appcontext_popped

from flask import Flask,render_template,g
from flask import signals
app = Flask(__name__) @signals.appcontext_popped.connect
def f14(app):
print('f14') @app.route('/index/')
def index():
return render_template('index.html') @app.route('/order')
def order():
print('order')
return render_template('order.html') if __name__ == '__main__':
app.run()

总结:关于flask内部共有14+个扩展点用于我们对flask框架内部进行定制,其中有:9个是信号。

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")
got_request_exception = _signals.signal("got-request-exception")
appcontext_tearing_down = _signals.signal("appcontext-tearing-down")
appcontext_pushed = _signals.signal("appcontext-pushed")
appcontext_popped = _signals.signal("appcontext-popped") message_flashed = _signals.signal("message-flashed")

最新文章

  1. 从myeclipse导入eclipse,不能识别为web项目(java项目转为web项目)
  2. 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划
  3. 1、java基础回顾与加强
  4. XtraBackup原理4
  5. JavaSE 国际化 简单例子
  6. Android使用Apache的httpmime包post提交数据
  7. Oracle 之——子查询 DDL DML 集合 及其他数据对象
  8. Python爬虫入门教程 20-100 慕课网免费课程抓取
  9. poj1416
  10. Alpha通道
  11. day28 1.缓冲区 2.subprocess 3.黏包现象 4.黏包现象解决方案 5.struct
  12. maven学习之一:maven安装
  13. Permission denied (publickey),Gitlab & Github 多ssh key 冲突 导致的权限问题
  14. Facebook广告API系列 3 Ads Management
  15. poj 2299 Ultra-QuickSort(树状数组求逆序数)
  16. [html][javascript] 正则匹配示例
  17. python中的内容编码
  18. 【Treeview】遍历本地磁盘
  19. GITLAB服务基础
  20. CS API 测试3

热门文章

  1. 初接触matplotlib
  2. windows Server 2016安装Sqlserver远程连接的坑
  3. matplotlib学习日记(八)----完善统计图
  4. mybatis实现MySQL数据库的增删改查
  5. 项目中同一个页面引入不同的jQuery版本的不冲突问题
  6. spring 切面织入报错:java.lang.ClassCastException: com.sun.proxy.$Proxy7 cannot be cast to...
  7. String 类的常用方法都有那些?
  8. Java学习日报8.6
  9. maven仲裁机制
  10. 自动化运维工具-Ansible之7-roles