当前位置:网站首页>P35-P41 fourth_ context
P35-P41 fourth_ context
2022-07-03 04:17:00 【Suyuoa】
The context is flask A unique concept ,falsk Medium request Objects are global variables , In different threads, we get different responses , For example, this one below us
Context is divided into request context and application context , The above is the request context request, except request,session It is also a request context
Application context has current_app And g object , We haven't used these two before
Catalog
1 current_app
current_app You can return the current application object , Let's write a code to see
- In order to show clearly, we have created a new project named fourth_context
We have created two application objects , Then add an attribute to each application , Then give them a view , Then in the code we run app1
The other runs in the terminal , Run in terminal app2 Change the environment variable first , The environment variable should be changed like this
Enter after activating the environment flask run
We are now visiting 127.0.0.1:5000/home2 Will show app2
If you visit 127.0.0.1:5001/home1 Will show app1
Use current_app Is the purpose of , When it's inconvenient for me to deal with a app When the object , I can use current_app To deal with
When is it inconvenient , At this time, let's take an example
Now we want to app1 Make a blueprint
Then we have to create a blueprint package , And then in __init__ writes
At this time, I found app1 Not imported in file , So you can write like this
This change is a little troublesome , It's not flexible , Then we have a more flexible way of writing , We can write this way
Let's measure , No problem found
2 g object
g The object is flask A temporary variable in , We can pass some data between multiple functions in one request , Every request resets this variable
Let's take an example
Use g object , We can do that func1() Use in g_view() Properties defined
Let's visit
Discovery can be successfully obtained
Later courses (P38-P39) It is about making a certification with a decorator , I don't think it makes sense , So there's no record
P40 stay python console Use context in mode , I don't think it makes any sense
P41 It refers to the internal principle of context , It roughly means that each thread will have another corresponding variable , Then it briefly introduces a project , There is nothing to write
边栏推荐
- [set theory] set concept and relationship (set represents | number set | set relationship | contains | equality | set relationship property)
- 【毕业季·进击的技术er】职场人的自白
- [NLP]—sparse neural network最新工作简述
- Dismantle a 100000 yuan BYD "Yuan". Come and see what components are in it.
- [brush questions] most elements (super water king problem)
- Feature_selection
- Xrandr modify resolution and refresh rate
- Fcpx template: sweet memory electronic photo album photo display animation beautiful memory
- [fairseq] 报错:TypeError: _broadcast_coalesced(): incompatible function arguments
- Idea shortcut keys
猜你喜欢
When writing a web project, SmartUpload is used for file upload and new string () is used for transcoding, but in the database, there will still be random codes similar to poker
GFS分布式文件系统(光是遇见已经很美好了)
IPv6 foundation construction experiment
JS realizes lazy loading of pictures
[pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius
MPLS setup experiment
[brush questions] most elements (super water king problem)
Bisher - based on SSM pet adoption center
有监督预训练!文本生成又一探索!
540. Single element in ordered array
随机推荐
Export of zip file
【毕业季·进击的技术er】职场人的自白
JS realizes lazy loading of pictures
Classes in TS
Idea shortcut keys
BMZCTF simple_ pop
『期末复习』16/32位微处理器(8086)基本寄存器
How to connect WiFi with raspberry pie
Two points -leetcode-540 A single element in an ordered array
解决bp中文乱码
Nat. Comm. | use tensor cell2cell to deconvolute cell communication with environmental awareness
[literature reading] sparse in deep learning: practicing and growth for effective information and training in NN
2022deepbrainchain biweekly report no. 104 (01.16-02.15)
[Yu Yue education] reference materials of political communication science of Communication University of China
sklearn数据预处理
[brush questions] connected with rainwater (one dimension)
Competitive product analysis and writing
MySQL field userid comma separated save by userid query
MPLS setup experiment
[untitled] 2022 safety production supervisor examination question bank and simulated safety production supervisor examination questions