1.ThreadLocal cause
Imagine a scenario where multiple users in flask have initiated multiple HTTP access request requests, and each HTTP request is a separate thread, then we have many threads that must be independent between them to ensure that the data for each request is correct and independent.
Well, we're sure that each thread's request is a separate local variable object ,
1. Method of transfer : it is passed as a parameter to the function processing, however, with the increase in business and functionality, there may be many functions to handle the request, if each is passed with parameters, it is too cumbersome, too much code, modification is cumbersome, very unfriendly.
2. Non-pass mode : But if the use of global variables, directly by the function processing, the function does not use the way of the argument, an intuitive way is to establish a global dictionary, the process ID to save the process to the local variables mapping, the running thread can be based on their own ID to get the data you own. In this way, you can avoid passing parameters in a function call, such as the following example:
global_data = {} def Show (): Cur_thread = Threading.current_thread () print Cur_thread.getname (), Global_data[cur_thread] def thread_cal (): Cur_thread =< Span style= "COLOR: #000000" > Threading.current_thread () Global_data[cur_thread] = 0 for _ in xrange (1000 ): Global_data[cur_thread] + = 1 show () Span style= "COLOR: #008000" ># need no local variable. Looks good. ...
It is not perfect to save a global dictionary and then use the thread identifier as the key and the local data of the corresponding thread as value. First, each function needs to obtain its own thread ID, which is slightly cumbersome, when it requires thread-local data. Worse, there is no real separation of data between threads, because each thread can read a global dictionary, and each thread can make changes to the contents of the dictionary.
3.ThreadLocal: to better solve this problem, the Python line libraries implements the ThreadLocal variable (many languages have similar implementations, such as Java). ThreadLocal really does the data isolation between threads and does not need to manually get its own thread ID when used, as in the following example:
Global_data =threading.local ()defShow ():PrintThreading.current_thread (). GetName (), Global_data.numdefthread_cal (): Global_data.num=0 for_inchXrange (1000): Global_data.num+ = 1Show () Threads= []... Print "Main Thread:", Global_data.__dict__ # {}
In the above example, the global variable global_data is a Threadlocal class object, each thread can obtain its own unique data through global_data.num, isolation from each other, each thread reads the global_data is different, It seems that Global_data has a lot of copies, really do the isolation between threads, threadlocal objects are as simple as global variables, but have the data isolation characteristics of local variables, so it is a very special and powerful class.
Python Special Thread: ThreadLocal