1、實例化補充python
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:\Users\Administrator\PycharmProjects\s6day120\1.實例化補充 # 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的文件夾flask
2、信號(blinker)session
一、flask的內置信號app
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)和信號有什麼區別?測試
- 觸發信號是沒有返回值的,寫不寫返回值都無所謂spa
- 特殊的裝飾器對返回值是有意義的,當before_request有返回值時就不會執行後續視圖函數了,沒有返回值的時候纔會執行後續函數,而after_request必須有返回值 debug
因此特殊裝飾器的功能比信號的功能強大code
問題2:經過信號能夠作權限嗎?
- 自己是作不了的,要想作得用其餘的機制配合着來使用,這樣作的話會閒的很麻煩,因此咱們選擇中間件來作
問題3:信號用於作什麼呢?
- 只作一些自定義的操做,並且沒有返回值
- 下降代碼之間的耦合
flask內置信號源碼詳細
def full_dispatch_request(self): # 三、特殊的裝飾器:執行被@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) #四、執行@after_request裝飾的全部的函數,session保存 return self.finalize_request(rv)
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
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
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)
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) #請求上下文執行完畢後自動執行(不管成功與否)
二、自定義信號(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}
3、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]