zoukankan      html  css  js  c++  java
  • Flask 【第六篇】Flask实例化参数以及信号

    一、实例化补充

    instance_path和instance_relative_config是配合来用的、
    这两个参数是用来找配置文件的,当用app.config.from_pyfile('settings.py')这种方式导入配置文件的时候会用到
    from flask import Flask,request
    app = Flask(__name__,instance_path=None, instance_relative_config=True)
    app.config.from_pyfile('settings.py') # C:UsersAdministratorPycharmProjectss6day1201.实例化补充
    # instsnce_path:#如果配置了instance_path,就会去找instance里面的文件
    # instance_relative_config: #如果设置为True,配置文件就找不到了,就会去找instance里面的settings.py
    app.open_session
    print(app.config.get("NNN"))
    @app.route('/index') # app.route('/index')  f(index)
    def index():
        print(request)
        return "xx"
    
    if __name__ == '__main__':
        app.__call__
        app.run()

     如果设置了instance——releative_config = True,就找不着settings.py文件了,解决办法:就手动创建一个instance的文件夹

    二、信号(blinker)

    1、flask的内置信号

     Flask框架中的信号基于blinker,其主要就是让开发者可是在flask请求过程中定制一些用户行为。说白了也就是flask在列表里面

    预留了几个空列表,在里面存东西。信号通过发送通知来帮助你解耦应用。简言之,信号允许某个发送者通知接收者有事情发生了;、

      

    10个信号:
     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在其中添加数据时,自动触发

    问题1:特殊的装饰器(@app.before_first_request ;@app.before_request ; @app.after_request和信号有什么区别?

       -  触发信号是没有返回值的,写不写返回值都无所谓

       -  特殊的装饰器对返回值是有意义的,当before_request有返回值时就不会执行后续视图函数了,没有返回值的时候才会执行后续函数,而after_request必须有返回值 

             所以特殊装饰器的功能比信号的功能强大

    问题2:通过信号可以做权限吗?

       - 本身是做不了的,要想做得用其他的机制配合着来使用,这样做的话会闲的很麻烦,所以我们选择中间件来做

    问题3:信号用于做什么呢?

       - 只做一些自定义的操作,而且没有返回值

         - 降低代码之间的耦合

    flask内置信号源码详细

    appcontext_pushed和 appcontext_popped
    request_started
    def full_dispatch_request(self):
            # 3、特殊的装饰器:执行被@before_first_request装饰的所有函数
            self.try_trigger_before_first_request_functions()
            try:
                request_started.send(self)   #请求到来前执行信号
                #执行@before_request装饰的所有函数
                rv = self.preprocess_request()
                if rv is None:
                    #如果return None就去执行后续视图函数
                    rv = self.dispatch_request()
            except Exception as e:
                rv = self.handle_user_exception(e)
            #4、执行@after_request装饰的所有的函数,session保存
            return self.finalize_request(rv)
    before_render_template和template_rendered
    def _render(template, context, app):
        """Renders the template and fires the signal"""
    
        before_render_template.send(app, template=template, context=context) #模板渲染前执行
        rv = template.render(context)
        template_rendered.send(app, template=template, context=context)  #模板渲染后执行
        return rv
    request_finished
    def finalize_request(self, rv, from_error_handler=False):
            response = self.make_response(rv)
            try:
                response = self.process_response(response)
                request_finished.send(self, response=response)
            except Exception:
                if not from_error_handler:
                    raise
                self.logger.exception('Request finalizing failed with an '
                                      'error while handling an error')
            return response
    got_request_exception
    def handle_exception(self, e):
            exc_type, exc_value, tb = sys.exc_info()
    
            got_request_exception.send(self, exception=e)   #出现异常时触发的信号
            handler = self._find_error_handler(InternalServerError())
    
            if self.propagate_exceptions:
                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 self.finalize_request(handler(e), from_error_handler=True)
    request_tearing_down
    appcontext_tearing_down
    def do_teardown_appcontext(self, exc=_sentinel):
      
            if exc is _sentinel:
                exc = sys.exc_info()[1]
            for func in reversed(self.teardown_appcontext_funcs):
                func(exc)
            appcontext_tearing_down.send(self, exc=exc)  #请求上下文执行完毕后自动执行(无论成功与否)
    

      

    2、自定义信号(Blinker的使用)

      第一步:创建信号

      第二步:将函数注册到信号中: 添加到这个列表

      第三步: 发送信号

      第四步:运行

    具体实现:可参考flask源码,写一个自定义信号

    from flask import Flask,flash
    from flask.signals import _signals
    app = Flask(__name__)
    
    xinhao = _signals.signal("xinhao")#创建信号
    #定义函数
    def wahaha(*args,**kwargs):
        print("娃哈哈",args,kwargs)
    
    def sww(*args,**kwargs):
        print("爽歪歪",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)
        
    #打印结果
    # 娃哈哈 (None,) {'sender': 'xxx', 'a1': 123, 'a2': 456}
    # 爽歪歪 (None,) {'sender': 'xxx', 'a1': 123, 'a2': 456}
    三、chain模块简单的测试
    v1 = [11,22,33,44]
    v2 = [1,4,7,5]
    from itertools import chain
    ff = []
    for i in chain(v1,v2):   #chain会把两个列表连接在一块
        ff.append(i)
    print(ff)     #[11, 22, 33, 44, 1, 4, 7, 5]
     
  • 相关阅读:
    C语言多文件参数传递
    第十章 C++11新特性
    第九章 STL标准库(二)
    第八章 标准模板库STL(一)
    第七章 2.泛型编程(模板)
    第七章 1.输入输出与模板
    第六章 多态
    第五章 继承与派生
    第四章 运算符重载
    第三章 类与对象进阶
  • 原文地址:https://www.cnblogs.com/xiaohema/p/8456736.html
Copyright © 2011-2022 走看看