当前位置:网站首页>[flask advanced] combined with the source code, explain the operation mechanism of flask (in and out of the stack)
[flask advanced] combined with the source code, explain the operation mechanism of flask (in and out of the stack)
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 !

Catalog
Preface
In the last section, we took a look at flask Source code of context , Sort out the flask The core object ,Request And context , And it mentioned at the end flask The agency model of LocalProxy, In this section, let's take a look at it on this basis flask Operation mechanism of .
analysis
about flask Operation mechanism of , Here is a clear picture :

When a request enters , A request context will be generated RequestContext, Enclosed inside Request It contains all kinds of information about this request . When the request is encapsulated ,flask This request will be pushed into the stack structure , That is, in the source code of the previous blog post push Method .
flask of use LocalStack This object represents the stack ,LocalStack After this object is instantiated , For request context , use _request_ctx_stack This variable represents ; For application context , use _app_ctx_stack This variable represents .
We can find it in the source file globals.py Found in file :
Okay , This completes the request context RequestContext Push , Now let's talk about application context AppContext, This is where... Is involved flask A logic of ,RequestContext The application context will be checked before entering the stack _app_ctx_stack Whether the top of the stack is empty , If the top of the stack is empty , be flask Will put the application context AppContext Push into stack , Then we will RequestContext Push to _request_ctx_stack In the stack .
After finishing these two contexts , Let's see current_app and request Implementation principle of , These two variables actually point to the top of the stack , When you go to use current_app and request these two items. LocalProxy ( agent ) when , It is the indirect operation of the top elements of these two stacks , And these two elements at the top of the stack are context , When these two stacks are not empty , Then we use current_app and request Just normal .
current_app Points to the application context _app_ctx_stack The top of the stack , The top of the stack is AppContext, but current_app The return is indeed flask Core objects of app, We can analyze it from the source code :
current_app: "Flask" = LocalProxy(_find_app) # type: ignore
You can see current_app Is instantiating a LocalProxy , It takes a parameter _find_app,_find_app Is a function , We ctrl+B Take a look at the source code of this function :
def _find_app():
top = _app_ctx_stack.top
if top is None:
raise RuntimeError(_app_ctx_err_msg)
return top.app
It's simple , This function takes _app_ctx_stack Top element of , The final return is in the top element of the stack app, Application context AppContext Inside flask The core object .
request Is the same , It doesn't end up with a request context RequestContext, Instead, request context RequestContext Inside Request object , Let's also take a brief look at the source code :
request: "Request" = LocalProxy(partial(_lookup_req_object, "request")) # type: ignore
In this partial A string is passed in “request”, also _lookup_req_object This function , In fact, this string is passed as a parameter to _lookup_req_object This function , Let's also look at what this function does :
def _lookup_req_object(name):
top = _request_ctx_stack.top
if top is None:
raise RuntimeError(_request_ctx_err_msg)
return getattr(top, name)
You can see that it is still taken _request_ctx_stack The top element of the stack , The final return is under the element at the top of the stack name attribute ,name It's the incoming "request" character string .
When the request is over , These two context objects will pop up from the corresponding stack , That is, the analysis in the last blog pop Method !
Last blog post :【flask senior 】 Understand deeply from the source code flask Application context and request context
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
边栏推荐
- Install MySQL database version 5.7.29 under ubuntu20.04 system
- Gan, why '𠮷 𠮷'.Length== 3 ??
- 使用Numpy进行高程统计及可视化
- 3. Like you, DNS domain name resolution service!!!
- HCIP实验(02)
- 【蓝桥杯集训100题】scratch太极图 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第22题
- The most comprehensive UE4 file operation in history, including opening, reading, writing, adding, deleting, modifying and checking
- Flask框架——Flask-WTF表单:文件上传、验证码
- Learn NLP with Transformer (Chapter 8)
- 数字孪生万物可视 | 联接现实世界与数字空间
猜你喜欢

Introduction to onnx runtime

JS hash table 01

NB-IOT控制液晶屏(日期的设置与读取)

2021 牛客网笔试总结 01

最详细的mysql索引解析(文末附赠思维导图)

2021 written examination summary of niuke.com 01

【策略模式】就像诸葛亮的锦囊

The idea has been perfectly verified again! The interest rate hike is approaching, and the trend is clear. Take advantage of this wave of market!

AI技术栈太庞大!吴恩达给出职业生涯规划:终生学习

Configuration of OSPF protocol (take Huawei ENSP as an example)
随机推荐
How to optimize the performance when the interface traffic increases suddenly?
[high concurrency] how to realize distributed flow restriction under 100 million level traffic? You must master these theories!!
哥廷根大学提出CLIPSeg:一个使用文本和图像prompt能同时作三个分割任务的模型
Introduction to onnx runtime
从开源的视角,解析SAP经典ERP “三十年不用变”的架构设计
Flask framework - session and cookies
Probe into Druid query timeout configuration → who is the querytimeout of datasource and jdbctemplate effective?
性能测试中TPS的计算【杭州多测师】【杭州多测师_王sir】
redis 哨兵,高可用的执行者
BGP联邦实验
[servlet] request parsing
I, AI doctoral student, online crowdfunding research topic
Use three.js to realize the cool cyberpunk style 3D digital earth large screen
I wrote code for openharmony, and the second phase of "code" pioneer officially opened!
Cloud native ide: the first general codeless development platform of IVX for free
树形动态规划
The integration of two in one has a long way to go
[strategic mode] like Zhugeliang's brocade bag
微信小程序版本更新如何通知用户?
HCIA实验(10)NAT