30. The meaning of Localstack as a thread-isolating object 30.1 data structures limit some capabilities 30.2 thread isolation
#-*-coding=utf-8-*-ImportThreadingImport Time fromWerkzeug.localImportLocalstackmy_stack=Localstack () My_stack.push (1)Print('in main thread after push, value is:'+str (my_stack.top))defworker ():#New Thread Print('in new thread before push, value is:'+str (my_stack.top)) My_stack.push (2) Print('in new thread after push, value is:'+str (my_stack.top)) new_t= Threading. Thread (Target=worker, name='Qiyue_thread') New_t.start () Time.sleep (1)
test/localstack.py
30.3 flask Why need stack structure
Requires a stack structure to push two contexts into the stack
30.4 Why it is necessary for localstack to require that the stack be thread-isolated 30.5 thread isolation meaning that the current thread is able to correctly reference objects created by himself, rather than referencing objects created by other threads (1) with a variable name (request) colleague pointing to multiple threads It is not possible to build multiple Instanced objects (2) can be done, in the current thread, in reference to the request (variable name) can correctly find the current thread itself is instantiated by the request object 30.6 requests the context contains the requestor object, so, ask is also thread-isolated s Ession is also thread-isolated 30.7 knowledge grooming (1) thread-isolated objects Localstack and local are thread-isolated objects (2) thread-isolated objects create thread-isolated objects through thread-isolated objects (3) Flask Core Objects App as a property exists in Appcontext application context the core Object app has only one, is created in the portal file, is created in the main thread (4) Multithreaded programming difficulty threads thread Isolation 30.8 Summary (1) LOCAL-&G T Localstack, the thread isolation object implementation has a dictionary inside the local, how to implement the thread ID number as key localstack? Localstack encapsulates the local operation of local, which is typically used. To access the following properties, using Localstack, you need to use several common methods and properties, push, Pop, top (2) appcontext-> RequestContext requests come in, will be pushed into the localstack stack, and at the end of the request, AppContext and RequestContext will be ejected by pop (3) flask->appcontext reques T->requestcontext Appcontext Important features, the flask core object as one of its properties, saved up RequestContext request context, the request object requested package and save (4) Current _app-> (Localstack.top=appcontext Top.app=flask) Current_app points to the top element of the stack below LocalstackA property, the top element of the Top.app,flask core object stack, is the application context (5) request-> (Localstack.top=requestcontext top.request=request) requ EST actually points to the request requests object under the top element of the Localstack stack
Flask Advanced Programming-localstack thread isolation