In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to understand ThreadLocal variables in Python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand ThreadLocal variables in Python".
Using global variables alone does not meet the needs of a multithreaded environment, and many times threads need to have their own private data, which is invisible to other threads. Therefore, local variables can also be used in threads, which can only be accessed by the thread itself, but not by other threads under the same process.
Sometimes it is not convenient to use local variables, so python also provides the ThreadLocal variable, which itself is a global variable, but each thread can use it to hold its own private data, which is not visible to other threads. The following figure shows the existence of these variables in the thread:
Thread variable
Global VS local variables
First of all, with the help of a Mini Program to look at the synchronization of global variables in a multithreaded environment.
Import threading global_num = 0 def thread_cal (): global global_num for i in xrange (1000): global_num + = 1 # Get 10 threads, run them and wait them all finished. Threads = [] for i in range (10): threads.append (threading.Thread (target=thread_cal)) thread [I] .start () for i in range (10): thread [I] .join () # Value of global variable can be confused. Print global_num
Here we have created 10 threads, each of which adds 1 to the global variable global_num 1000 times (loop 1000 plus 1 is to extend the execution time of a single thread and cause the thread to be interrupted and switched during execution). What is the value of the global variable when the 10 threads are finished? The answer is no. To put it simply, because global_num + = 1 is not an atomic operation, execution may be interrupted by other threads, causing other threads to read a dirty value. Take two thread execution + 1 as an example, one of the possible execution sequences is as follows (in this case, the * * result is 1):
Multi-thread global variable synchronization
This problem is common when using global variables in multithreading, and the solution is simple, using mutexes, conditional variables, or read-write locks. Below consider using mutex to solve the problem of the above code, only need to add the lock before the + 1 operation, and release the lock after the operation, so that the atomicity of the operation can be guaranteed.
L = threading.Lock ()... L.acquire () global_num + = 1 l.release ()
Using local variables in threads does not have this problem, because the local variables of each thread cannot be accessed by other threads. Below, we use 10 threads to perform 1000 plus 1 operations on their local variables, and print the total number of operations performed at the end of each thread (1000 for each thread):
Def show (num): print threading.current_thread (). GetName (), num def thread_cal (): local_num = 0 for _ in xrange (1000): local_num + = 1 show (local_num) threads = [] for i in range (10): threads.append (threading.Thread (target=thread_cal)) thread [I] .start ()
You can see that each thread here has its own local_num, and each thread does not interfere with each other.
Thread-local object
There is nothing wrong with passing the local_num local variable to the show function in the above program. However, considering that in the actual production environment, we may call a lot of functions, each function requires a lot of local variables, it is very unfriendly to pass parameters.
In order to solve this problem, an intuitive way is to establish a global dictionary to save the mapping relationship between the process ID and the local variables of the process, and the running thread can get its own data according to its own ID. In this way, you can avoid passing parameters in a function call, as shown in 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 = threading.current_thread () global_ data [cur _ thread] = 0 for _ in xrange (1000): global_ data [cur _ thread] + = 1 show () # Need no local variable. Looks good. ...
It is not * to save a global dictionary and then use the thread identifier as key and the corresponding thread's local data as value. First of all, when each function needs thread local data, it needs to get its own thread ID first, which is slightly cumbersome. To make matters worse, there is no real data isolation between threads, because each thread can read the global dictionary, and each thread can change the contents of the dictionary.
To better solve this problem, the python thread library implements ThreadLocal variables (many languages have similar implementations, such as Java). ThreadLocal really achieves data isolation between threads, and does not need to manually obtain its own thread ID when using it, as shown in the following example:
Global_data = threading.local () def show (): print threading.current_thread () .getName (), global_data.num def thread_cal (): global_data.num = 0 for _ in xrange (1000): global_data.num + = 1 show () threads = []. Print "Main thread:", global_data.__dict__ # {}
In the above example, each thread can get its own unique data through global_data.num, and each thread reads a different global_data, which is really isolated between threads.
Thank you for your reading, the above is the content of "how to understand the ThreadLocal variable in Python". After the study of this article, I believe you have a deeper understanding of how to understand the ThreadLocal variable in Python. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.