In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you the example analysis of mutex lock and deadlock in python multithreading. I hope you will get something after reading this article. Let's discuss it together.
1. Resource competition among multithreads
Take the following functions task1 () and task2 () as examples, which will repeat the global variable num 10 million times respectively (if the data is large or too small, it will be executed too fast to achieve the effect of verification).
Import threadingimport timenum = 0def task1 (nums): global num for i in range (nums): num + = 1 print ("task1---num=%d"% num) def task2 (nums): global num for i in range (nums): num + = 1 print ("task2---num=%d"% num) if _ name__ ='_ main__': nums = 10000000 T1 = threading.Thread (target=task1, args= (nums) ) T2 = threading.Thread (target=task2, args= (nums,)) t1.start () t2.start () # because the main thread will not wait for the child thread to finish execution So there is a five-second delay to ensure the final execution. Time.sleep (5) print ("main----num=%d"% num)
The result of running the program:
As shown in the figure, the output is confusing, neither 10 million nor 20 million. Because there is resource competition at multithreaded runtime, that is, it can be understood that the time for each function to run is uncertain and influence each other.
For example, starting from the initial value of 0, suppose the thread of T1 executes first, and after executing to + 1, the num=1 has not yet been stored, then it is stopped, T2 begins to execute, to obtain num, the obtained num is equal to the initial value 0, then it executes + 1 and stores, num=1 after storage, and then T2 stops T1 to continue and store num=1 again. That is, add 1 twice, but num is still only equal to 1.
Because the allocation of T1 and T2 who will run is completely random, the value is less than 20 million after adding 20 million times.
Mutexes can be used to solve such problems.
2. Mutex
When a thread wants to change the shared data, it is locked first. At this time, the state of the resource is "locked" and other threads cannot change it. Only when the thread releases the resource and changes the state of the resource to "unlocked" can other threads lock the resource again.
The mutex ensures that only one thread writes at a time, thus ensuring the correctness of data in the case of multiple threads.
1. Example of mutex lock
Create a lock:
Mutex = threading.Lock () mutex.acquire () # Lock the content locked by xxxx xxxxxmutex.release () # unlock
Add the mutex to the above code as follows, and the problem is solved.
Import threadingimport timenum = 0def task1 (nums): global num mutex.acquire () for i in range (nums): num + = 1 mutex.release () print ("task1---num=%d"% num) def task2 (nums): global num mutex.acquire () for i in range (nums): num + = 1 mutex.release () print ("task2---num=%d"% num) if _ name _ _ ='_ _ main__': nums = 10000000 mutex = threading.Lock () T1 = threading.Thread (target=task1 Args= (nums,) T2 = threading.Thread (target=task2, args= (nums,)) t1.start () t2.start () # because the main thread will not wait for the child thread to finish execution So there is a five-second delay to ensure the final execution. Time.sleep (5) print ("main----num=%d"% num)
The result of running the program:
two。 Reentrant lock and non-reentrant lock
Threading.Lock () is a non-reentrant lock, that is, only one lock can be added at a time, not more.
Threading.Lock ()
If you need to add more places at the same time, you need to add a non-reentrant lock.
Create a reentrant lock:
Mutex = threading.RLock () mutex.acquire () # Lock mutex.acquire () # then lock xxxx locked content xxxxxmutex.release () # unlock mutex.release () # unlock again
The number of locking and unlocking must be the same.
Third, deadlock
When sharing multiple resources between threads, if two threads occupy part of the resources and wait for each other's resources at the same time, the program will be blocked, resulting in deadlock.
Deadlocks are usually not needed.
Programming should be avoided as much as possible.
After reading this article, I believe you have some understanding of "sample analysis of mutex and deadlock in python multithreading". If you want to know more about it, please follow the industry information channel and thank you for your reading!
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.