Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of context Management of Flask

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "detailed explanation of context Management of Flask". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Flask inter-thread context security

The principle of Falsk thread safety is to maintain the request stack and app stack in the process after startup, and the stack ensures the thread safety of each request through thread ID.

The implementation mainly depends on three classes: Local,LocalStack and LocalProxy. Let's take a look at the specific implementation principle.

Three classes build local data

1. Local

First look at the Local source code, the essence is not defined in Flask, but Flask depends on the definition of the werkzeug library.

You can see its two defined properties, _ _ storage__, _ _ ident_func__, and three methods _ _ getattr__,__setattr__,__release_local__.

1. The attribute _ _ storage__ is a multi-level dictionary. The first layer key is the implicit thread ID or ID, and the second layer key is the actual keyword used.

two。 The attribute _ _ ident_func__ can be seen as the get_ident function. The get_ident function either obtains the thread ID of the current execution unit through the thread library, or obtains the ID of the current execution program through the greenlet library.

In addition, you can see that the three methods of the Local class are essentially to achieve data isolation between threads or coprograms by rewriting the Python built-in functions _ _ setattr__ and _ _ getattr__.

1. When getting an attribute of local, such as: local.age essentially triggers the _ _ getattr__ method to get the ID--__ident_func__ function of the current thread, then find the result set corresponding to the thread ident in the _ _ storage__ dictionary, and then find the age attribute from the obtained result.

two。 When setting an attribute of local, for example: local.age = 12 actually triggers the _ _ setattr__ method to get the ID--__ident_func__ function of the current thread, and then sets the corresponding attribute dictionary set in the _ _ storage__ dictionary

Another _ _ release_local__ method is to delete the corresponding thread data.

It is more intuitive to draw a schematic diagram.

An object local=Local () is generated in the main thread, and the number of local.no= corresponding to each thread performing the same operation in three threads. Open up a store for each thread, so whoever fetches or saves it will find its own corresponding location. Although getting the key is the same, each access is only about its own value.

2. LocalStack

LocalStack is also defined in the werkzeug library that Flask depends on. Literally, it is the stack operation of Local. See how the source code is defined.

LocalStack essentially operates around Local. According to the above, after reading the source code of Local, we can see

LocalStack defines a Local object

A stack property is set for this object, and this property is a list

LocalStack defines methods to stack and unstack the list.

Provides a method of customizing ident_func for the Local object in the class

3. LocalProxy

LocalProxy literally means to be a proxy for Local. Let's first look at the use of LocalProxy from a definition of request, and then combine the source code to see what LocalProxy is used for.

What's slightly harder to understand in the class is the role of object.__setattr__ (self, "_ LocalProxy__local", local), which is actually setting a _ _ local property for self. This is the definition of private variables in the Python class. You can take a look at the official definition of Python, the python private variable.

You can see that this class overrides all the built-in methods of the Python class, and all operations after the override are based on the objects returned by the _ get_current_object method defined in the class.

The return value in this method is the result of the execution of the local object given at initialization. If you do not specify a Local object when you create it, execute this method directly. If a Local object is given, the corresponding object is found based on the class name.

Now this is quite abstract. What on earth does this agent do? Let's look at it in conjunction with Flask to define a global request object. If we want to get the method of the request, then we are using request.method.

The following is the source code defined by request

According to the _ _ getattr__ method overridden in the source code of LocalProxy, first execute the _ get_current_object method to get the object, and then get the method property of the returned object.

The function passed when creating LocalProxy is the partial function of _ lookup_req_object, which is actually _ lookup_req_object and name=request

In LocalProxy, _ _ local is a function, so to execute _ get_current_object is to execute _ lookup_req_object and the value returned by name=request, and then take its method attribute

Execute the _ lookup_req_object function at this time to get the request of top from _ request_ctx_stack

Using Proxy, you can easily and quickly use request.method to get the corresponding value, the core of which is that the corresponding function is executed every time you get it, and the value returned in the function is thread-safe. Ensure that the data is correct and elegant. Otherwise, we will execute a function every time to get its value, and then take its properties.

This is the end of the content of "context Management details of Flask". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report