程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python multithread deadlock phenomenon and its solution

編輯:Python

Catalog

The point of this section

One Deadlock

Two Recursive lock

The point of this section

Understand deadlock phenomena and Solutions

The duration of this section needs to be controlled 15 Within minutes

One Deadlock

The so-called deadlock : Refers to two or more processes or threads in the execution process , A phenomenon of waiting for each other caused by competing for resources , If there is no external force , They will not be able to move forward .

At this point, the system is said to be in a deadlock state or the system has produced a deadlock , These processes that are always waiting for each other are called deadlock processes , Here is deadlock

from threading import Thread,Lockimport timemutexA=Lock()mutexB=Lock()class MyThread(Thread): def run(self): self.func1() self.func2() def func1(self): mutexA.acquire() print('\033[41m%s Get A lock \033[0m' %self.name) mutexB.acquire() print('\033[42m%s Get B lock \033[0m' %self.name) mutexB.release() mutexA.release() def func2(self): mutexB.acquire() print('\033[43m%s Get B lock \033[0m' %self.name) time.sleep(2) mutexA.acquire() print('\033[44m%s Get A lock \033[0m' %self.name) mutexA.release() mutexB.release()if __name__ == '__main__': for i in range(10): t=MyThread() t.start()

Execution effect

Thread-1 Get A lock Thread-1 Get B lock Thread-1 Get B lock Thread-2 Get A lock # Deadlock occurred , The whole program is blocked Two Recursive lock

resolvent , Recursive lock , stay Python To support multiple requests for the same resource in the same thread ,python Provides a re-entry lock RLock.

This RLock One is maintained internally Lock And a counter Variable ,counter Recorded acquire The number of times , So that the resource can be multiple times require. All the way to a thread acquire All be release, Only other threads can get the resource .

If the above example uses RLock Instead of Lock, No deadlock , The difference between them is : Recursive locks can be continuous acquire many times , And mutexes can only acquire once

from threading import Thread,RLockimport timemutexA=mutexB=RLock() # A thread gets the lock ,counter Add 1, In this thread, the situation of locking is encountered , be counter Keep adding 1, All other threads can only wait for , Wait for the thread to release all locks , namely counter Descending to 0 until class MyThread(Thread): def run(self): self.func1() self.func2() def func1(self): mutexA.acquire() print('\033[41m%s Get A lock \033[0m' %self.name) mutexB.acquire() print('\033[42m%s Get B lock \033[0m' %self.name) mutexB.release() mutexA.release() def func2(self): mutexB.acquire() print('\033[43m%s Get B lock \033[0m' %self.name) time.sleep(2) mutexA.acquire() print('\033[44m%s Get A lock \033[0m' %self.name) mutexA.release() mutexB.release()if __name__ == '__main__': for i in range(10): t=MyThread() t.start()

That's all python Details of multithread deadlock phenomenon and Solutions , More about python For information on solving multithread deadlock, please pay attention to other relevant articles on software development network !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved