当前位置:网站首页>[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
边栏推荐
- 6. PXE combines kickstart principle and configuration to realize unattended automatic installation
- C# 类库的生成,使用类库对象对DataGridView 进行数据绑定
- JS hash table 01
- What is the meaning of ordinary people's life?
- Modify MySQL group error expression 1 of select list is not in group
- Flame framework - Flame WTF form: file upload, verification code
- 【高并发】如何实现亿级流量下的分布式限流?这些理论你必须掌握!!
- Configuration of OSPF protocol (take Huawei ENSP as an example)
- Flask框架——Flask-WTF表单:数据验证、CSRF保护
- 微波技术基础实验一 滤波器的设计
猜你喜欢
Learning Weekly - total issue 63 - an open source local code snippet management tool

2021 written examination summary of niuke.com 01

HCIP(12)

Disabled and readonly and focus issues

Visual thematic map of American airport go style: ArcGIS Pro version

AI system frontier dynamics issue 43: ONEFLOW V0.8.0 officially released; GPU finds human brain connections; AI doctoral online crowdfunding research topic

Flask framework - Message flash
C# 类库的生成,使用类库对象对DataGridView 进行数据绑定

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

HCIP实验(01)
随机推荐
Druid 查询超时配置的探究 → DataSource 和 JdbcTemplate 的 queryTimeout 到底谁生效?
云原生IDE:iVX免费的首个通用无代码开发平台
Microwave technology homework course design - Discrete capacitance and inductance + microstrip single stub + microstrip double stub
JS bidirectional linked list 02
Understand the life cycle and route jump of small programs
Leetcode 560 prefix and + hash table
电磁场与电磁波实验一 熟悉Matlab软件在电磁场领域的应用
HCIP(12)
湖仓一体电商项目(二):项目使用技术及版本和基础环境准备
Software Testing Technology: cross platform mobile UI automated testing (Part 1)
Flask framework - session and cookies
Redis usage scenario
HCIA实验(06)
Learn NLP with Transformer (Chapter 3)
[high concurrency] how to realize distributed flow restriction under 100 million level traffic? You must master these theories!!
Keras深度学习实战(16)——自编码器详解
Flame framework - Flame WTF form: file upload, verification code
HCIA实验(07)综合实验
Keras deep learning practice (16) -- detailed explanation of self encoder
Electromagnetic field and electromagnetic wave experiment I familiar with the application of MATLAB software in the field of electromagnetic field