Flask 生命週期 - Flask life cycle
前言 完全不懂 Flask life cycle 我們也可以使用 Flask 建立出一個完整的系統,但是瞭解了之後,我們可以將各種資源的調度放在 Flask life cycle 的各個節點中,這樣可以讓整個系統更有條理,以下內容皆整理自 Flask 官方文件,我也會寫一個範例 Repo 幫助讀者更有效地理解 Flask life cycle。 https://flask.palletsprojects.com/en/3.0.x/lifecycle/ Flask handle request with WSGI server 以下步驟節錄自 Python 官方文件 Browser or other client makes HTTP request. WSGI server receives request. WSGI server converts HTTP data to WSGI environ dict. WSGI server calls WSGI application with the environ. Flask, the WSGI application, does all its internal processing to route the request to a view function, handle errors, etc. Flask translates View function return into WSGI response data, passes it to WSGI server. WSGI server creates and send an HTTP response. Client receives the HTTP response. 簡單說明: WSGI server 負責接收來自 Client 端的請求(step 1, 2),再將請求轉成 Python 可以執行的內容(step 3~5),讓 Python 處理完之後的內容打包 http response (step 6, 7)回應請求端(step8)。 ...