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 > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Is there any way to avoid deadlock? In view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
Ways to avoid deadlocks:
A deadlock occurs when two threads wait for each other to release resources. The Python interpreter does not monitor and does not take proactive measures to deal with deadlock situations, so measures should be taken to avoid deadlocks when doing multithreaded programming.
Once a deadlock occurs, the entire program does not have any exceptions or hints, but all threads are blocked and cannot continue.
Deadlocks can easily occur, especially if there are multiple synchronization monitors in the system, and deadlocks will occur in programs like this:
Import threadingimport timeclass A: def _ init__ (self): self.lock = threading.RLock () def foo (self B): try: self.lock.acquire () print ("current thread name: + threading.current_thread (). Name\ +" enters the foo () method of An instance) # ① time.sleep (0.2) print ("current thread name:" + threading.current_thread ( ). Name\ + "attempt to call the last () method of instance B") # ③ b.last () finally: self.lock.release () def last (self): try: self.lock.acquire () print ("inside the last () method of class A") Finally: self.lock.release () class B: def _ _ init__ (self): self.lock = threading.RLock () def bar (self A): try: self.lock.acquire () print ("current thread name: + threading.current_thread (). Name\ +" enters the bar () method of B instance) # ② time.sleep (0.2) print ("current thread name:" + threading.current_thread () .name\ + "attempt to call the last () method of An instance") # ④ a.last () finally: self.lock.release () def last (self): try: self.lock.acquire () print ("inside the last () method of class B") finally: Self.lock.release () a = A () b = B () def init (): threading.current_thread (). Name = "main thread" # call the foo () method of an object a.foo (b) print ("after entering the main thread") def action (): threading.current_thread (). Name = "secondary thread" # call the bar () method of the b object B.bar (a) print ("after entering the secondary thread") # start a new thread threading.Thread (target=action). Start () # call the init () function init () with action as the target
Run the above program and you will see the effect shown in figure 1.
Fig. 1 deadlock effect
As you can see from figure 1, the program has been "deadlocked" since it can neither execute down nor throw any exceptions. The reason is that the methods of An object and B object in the above program are thread-safe methods.
There are two threads in the program, the thread execution body of the secondary thread is the action () function, and the thread execution body of the main thread is the init () function (the main program calls the init () function). Let the B object call the bar () method in the action () function, and let the An object call the foo () method in the init () function.
Figure 1 shows that the action () function executes first, calling the bar () method of the B object, and the thread locks the Lock of the B object before entering the bar () method (when the program executes to the ② code, the secondary thread pauses for 0.2s). CPU switches to another thread, letting the An object execute the foo () method, so you can see that the main thread starts executing the foo () method of the An instance, and before entering the foo () method, the thread locks the Lock of the An object (when the program executes to the ① code, the main thread also pauses 0.2s).
Next, the secondary thread wakes up and continues to execute until it executes to the ④ code where you want to call the last () method of the An object (before executing this method, you must lock the Lock of the An object), but at this time the main thread is holding the lock on the Lock of the An object, so the secondary thread is blocked.
Then the main thread should wake up and continue to execute until it executes to the ③ code where you want to call the last () method of the B object (before executing this method, you must lock the Lock of the B object), but the secondary thread does not release the lock on the Lock of the B object.
At this point, the main thread keeps the lock on the An object, waiting for the lock on the B object, while the secondary thread keeps the lock on the B object, waiting for the An object to be locked, and the two threads wait for each other to release the lock first, so there is a deadlock.
Deadlocks should not occur in programs, and deadlocks should be avoided as far as possible when writing programs. There are several common ways to solve deadlock problems:
Avoid multiple locks. Try to avoid locking multiple Lock by the same thread. For example, in the above deadlock program, the main thread locks the Lock of An and B objects, and the secondary thread locks the Lock of An and B objects, which buries the hidden danger of deadlock.
Has the same locking order. If multiple threads need to lock multiple Lock, you should ensure that they request locks in the same order. For example, in the above deadlock program, the main thread locks the Lock of An object first, then the Lock of B object, while the secondary thread locks the Lock of B object first, and then locks the Lock of An object. This locking order can easily form a nested lock, which in turn leads to a deadlock. This problem can be avoided if the main thread and the secondary thread are locked in the same order.
Use timing locks. The program can specify the timeout parameter when calling the acquire () method to lock, which automatically releases the lock on Lock after more than timeout seconds, so that the deadlock can be unlocked.
Deadlock detection. Deadlock detection is a deadlock prevention mechanism based on algorithmic mechanism, which is mainly aimed at those scenarios where sequential locking and timing locking can not be realized.
This is the answer to the question about how to avoid deadlock. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.