Flask notes: Context

Thread isolated Thread Local:

If an object having a thread isolation characteristics, can be called "Thread Local", it is meant that the thread spacer object different threads are independent, in the operation of the object will not affect the other thread a thread the object operation, such as modifying the value of a property of the object a in the thread, but the property value of the object is not modified and the thread B.

 

Flask thread quarantined objects:

In Flask, the thread isolated objects include request, session, g, current_app, these objects can be imported directly use in the project, without fear of problems at different places at the same time using these objects at runtime. For example a plurality of different requests come in, which will be used to request the object, but because the request was isolated thread, and each thread has its own request, the request data object using the respective threads.

 

Request context and application context:

Application context and the request context has its own context, and context object LocalStack stack objects, when used in the context of an operation in view of the function (of CURRENT_APP objects, the url_for functions, etc.), it will automatically create context corresponding to the context object and push the corresponding LocalStack top of the stack, when the context of the operation, to automatically obtain the top stack LocalStack context object. Therefore, in view of the function can be operated using direct context.

If you want to use outside the context of an operation in view of the function, it is necessary to manually create the corresponding context object and push it into the corresponding LocalStack stack. A simple example code is as follows:

Create an application context manually:

from the Flask Import the Flask, current_app 

app = the Flask ( __name__ ) 

# The first way: Create a file directly in the context object, and push to LocalStack stack 
app_context = app.app_context () 
app_context.push () 
# Get the current app information current_app operation object belongs to the application context object 
Print (current_app) 


# the second way: use the application context with the block, this approach only operates in the context of the application with the block, would not be on the outside with 
# this after you create a context automatically push objects LocalStack way to stack 
with app.app_context ():
     Print (current_app)

 

Manually create the request context:

from Flask Import the Flask, the url_for 

App = the Flask ( the __name__ ) 


@ app.route ( ' / Detail / ' )
 DEF Detail ():
     return  ' ! Detail Information ' 


# will check the application LocalStack application context stack to the top there is no context object, if no, it will automatically create the application context context object and push the top of the stack to the application LocalStack 
# before you create the request context object and push the top of the stack to request LocalStack 
# Again, this method can only be with the block operation using the request context, with the outside would not be 
with app.test_request_context ():
     # the url_for inverted view acquired operation request context url belonging 
    Print (the url_for ( ' Detail ' ))

 

g Object:

Use "from flask import g" objects to use g (can be seen as the global abbreviation), g is a target object is used to store global data, and g is an object of the thread spacer. Usually some custom data we need to frequently used throughout Flask program, you can put g object, the time needed to get the g object. The definition of a good `g.username = 'xiaoming'` in a file, and then can be used directly to` g.username` in another file.

 

Guess you like

Origin www.cnblogs.com/guyuyun/p/11280041.html