当前位置:网站首页>[flask advanced] deeply understand the application context and request context of flask from the source code
[flask advanced] deeply understand the application context and request context of flask from the source code
2022-07-25 11:05:00 【Hall owner a Niu】
Personal profile
- Author's brief introduction : Hello everyone , I'm Daniel , New star creator of the whole stack .
- Blogger's personal website : A Niu's blog house
- Stand by me : give the thumbs-up + Collection ️+ Leaving a message.
- Series column :flask Framework quick start
- Maxim : To be light , Because there are people who are afraid of the dark !

Preface
Before that flask The introductory series probably wrote flask Application context and request context , It's more simple , But we need to know later flask Operation mechanism of , You must first have a deep understanding flask Application context and request context in , In this section, Daniel will continue to take you to analyze his mystery from the source code !
Context source code analysis
stay site-packages Find flask Folder , Find... In it ctx.py file , There are some context related contents !
Pictured above , Click the structure on the left , It's easy to find our application context AppContext and RequestContext.
and AppContext It's against us. flask An encapsulation of core objects , We can have a look AppContext Constructor of this class :
def __init__(self, app: "Flask") -> None:
self.app = app
self.url_adapter = app.create_url_adapter(None)
self.g = app.app_ctx_globals_class()
# Like request context, app contexts can be pushed multiple times
# but there a basic "refcount" is enough to track them.
self._refcnt = 0
You can see such a line of code : self.app = app, there app We passed app = Flask(name) Instantiated flask The core object ,flask Encapsulate this core object into AppContext.

Pictured above ,AppContext Four methods are defined below , These four methods are actually operations AppContext From stack to stack , I won't go into details here , Write... At the back flask We will also mention .
Next, let's look at the request context RequestContext


You can see RequestContext The constructor of is also applicable to our flask The core object is encapsulated , And it encapsulates request Request object !
We continue to slide down , Here's the picture :
We are RequestContext We also see four and AppContext In the same way ! It is also a stack structure
Thus summarize :Flask The core objects are stored in AppContext in ,Request Request object in RequestContext in !
Some people may have some doubts here , We operate this directly Flask Isn't the core object good , Why package , This actually involves our design pattern , Sometimes we design an object , But we found that some parameters are external to this object , At this time, we need to encapsulate these parameters and this object into a new object ,flask Just did such a thing , take Flask The core object and some external parameters are encapsulated into AppContext, Context object ,RequestContext So it is with !
summary
Read the source code , We need to find out now Flask, AppContext, Request, RequestContext The relationship between .
- Flask Is our core object , The core object carries various functions , For example, it saves the information of our configuration file , Provides registered routes 、 A series of functions such as view functions .
- AppContext take Flask The core object is encapsulated , And additional parameters are attached .
- Request Object holds our request information , For example, related url Parameters of .
- RequestContext take Request Objects are encapsulated , And additional parameters are attached .
We often use Flask Core objects and Request object , Through the understanding of the source code , Now we can think , We can not only pass from … import The import method uses Flask Core objects and Request object , And it can also be passed AppContext,RequestContext To indirectly obtain , So that's the point flask The core of the operation mechanism , Namely LocalProxy( agent ), Okay , Here is a brief mention , Let's continue in the next section !
Conclusion
If you think the blogger's writing is good , You can pay attention to the current column , Bloggers will finish this series ! You are also welcome to subscribe to other good columns of bloggers .
Series column
Soft grinding css
Hard bubble javascript
The front end is practical and small demo
边栏推荐
- 哥廷根大学提出CLIPSeg:一个使用文本和图像prompt能同时作三个分割任务的模型
- 【域泛化】2022 IJCAI领域泛化教程报告
- 【策略模式】就像诸葛亮的锦囊
- How to optimize the performance when the interface traffic increases suddenly?
- Flask framework -- flask caching
- 【蓝桥杯集训100题】scratch太极图 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第22题
- Introduction to onnx runtime
- Software Testing Technology: cross platform mobile UI automated testing (Part 1)
- I wrote code for openharmony, and the second phase of "code" pioneer officially opened!
- redis 哨兵,高可用的执行者
猜你喜欢
Qt | 鼠标事件和滚轮事件 QMouseEvent、QWheelEvent

Flask框架——Session与Cookie

2021 qunar written examination summary

Acquisition and compilation of UE4 source code

redis 哨兵,高可用的执行者

mysql事务是什么

Flask framework - flask WTF form: data validation, CSRF protection

从开源的视角,解析SAP经典ERP “三十年不用变”的架构设计

【flask高级】结合源码解决flask经典报错:Working outside of application context

JS collection
随机推荐
Last week's hot review (7.18-7.24)
微信小程序版本更新如何通知用户?
【flask高级】结合源码解决flask经典报错:Working outside of application context
我为OpenHarmony 写代码,战“码”先锋第二期正式开启!
美国机场围棋风格可视化专题图:ArcGIS Pro版本
DICOM medical image viewing and browsing function based on cornerstone.js
The practice of asynchronous servlet in image service
Probe into Druid query timeout configuration → who is the querytimeout of datasource and jdbctemplate effective?
Modify MySQL group error expression 1 of select list is not in group
2021 scenery written examination summary
The University of Gottingen proposed clipseg: a model that can perform three segmentation tasks simultaneously using text and image prompts
Learn NLP with Transformer (Chapter 6)
Google Earth Engine——统计逐年土地分类的频率
[domain generalization] 2022 IJCAI domain generalization tutorial Report
MySQL master-slave replication and read-write separation
淦,为什么 '𠮷𠮷𠮷' .length !== 3 ??
树形动态规划
HCIP(13)
HCIP(11)
The most comprehensive UE4 file operation in history, including opening, reading, writing, adding, deleting, modifying and checking