Python中死鎖的形成示例及死鎖情況的防止
死鎖示例
搞多線程的經(jīng)常會遇到死鎖的問題,學(xué)習(xí)操作系統(tǒng)的時候會講到死鎖相關(guān)的東西,我們用Python直觀的演示一下。
死鎖的一個原因是互斥鎖。假設(shè)銀行系統(tǒng)中,用戶a試圖轉(zhuǎn)賬100塊給用戶b,與此同時用戶b試圖轉(zhuǎn)賬200塊給用戶a,則可能產(chǎn)生死鎖。
2個線程互相等待對方的鎖,互相占用著資源不釋放。
#coding=utf-8
import time
import threading
class Account:
def __init__(self, _id, balance, lock):
self.id = _id
self.balance = balance
self.lock = lock
def withdraw(self, amount):
self.balance -= amount
def deposit(self, amount):
self.balance += amount
def transfer(_from, to, amount):
if _from.lock.acquire():#鎖住自己的賬戶
_from.withdraw(amount)
time.sleep(1)#讓交易時間變長,2個交易線程時間上重疊,有足夠時間來產(chǎn)生死鎖
print 'wait for lock...'
if to.lock.acquire():#鎖住對方的賬戶
to.deposit(amount)
to.lock.release()
_from.lock.release()
print 'finish...'
a = Account('a',1000, threading.Lock())
b = Account('b',1000, threading.Lock())
threading.Thread(target = transfer, args = (a, b, 100)).start()
threading.Thread(target = transfer, args = (b, a, 200)).start()
防止死鎖的加鎖機(jī)制
問題:
你正在寫一個多線程程序,其中線程需要一次獲取多個鎖,此時如何避免死鎖問題。
解決方案:
在多線程程序中,死鎖問題很大一部分是由于線程同時獲取多個鎖造成的。舉個例子:一個線程獲取了第一個鎖,然后在獲取第二個鎖的 時候發(fā)生阻塞,那么這個線程就可能阻塞其他線程的執(zhí)行,從而導(dǎo)致整個程序假死。 解決死鎖問題的一種方案是為程序中的每一個鎖分配一個唯一的id,然后只允許按照升序規(guī)則來使用多個鎖,這個規(guī)則使用上下文管理器 是非常容易實現(xiàn)的,示例如下:
import threading
from contextlib import contextmanager
# Thread-local state to stored information on locks already acquired
_local = threading.local()
@contextmanager
def acquire(*locks):
# Sort locks by object identifier
locks = sorted(locks, key=lambda x: id(x))
# Make sure lock order of previously acquired locks is not violated
acquired = getattr(_local,'acquired',[])
if acquired and max(id(lock) for lock in acquired) >= id(locks[0]):
raise RuntimeError('Lock Order Violation')
# Acquire all of the locks
acquired.extend(locks)
_local.acquired = acquired
try:
for lock in locks:
lock.acquire()
yield
finally:
# Release locks in reverse order of acquisition
for lock in reversed(locks):
lock.release()
del acquired[-len(locks):]
如何使用這個上下文管理器呢?你可以按照正常途徑創(chuàng)建一個鎖對象,但不論是單個鎖還是多個鎖中都使用 acquire() 函數(shù)來申請鎖, 示例如下:
import threading
x_lock = threading.Lock()
y_lock = threading.Lock()
def thread_1():
while True:
with acquire(x_lock, y_lock):
print('Thread-1')
def thread_2():
while True:
with acquire(y_lock, x_lock):
print('Thread-2')
t1 = threading.Thread(target=thread_1)
t1.daemon = True
t1.start()
t2 = threading.Thread(target=thread_2)
t2.daemon = True
t2.start()
如果你執(zhí)行這段代碼,你會發(fā)現(xiàn)它即使在不同的函數(shù)中以不同的順序獲取鎖也沒有發(fā)生死鎖。 其關(guān)鍵在于,在第一段代碼中,我們對這些鎖進(jìn)行了排序。通過排序,使得不管用戶以什么樣的順序來請求鎖,這些鎖都會按照固定的順序被獲取。 如果有多個 acquire() 操作被嵌套調(diào)用,可以通過線程本地存儲(TLS)來檢測潛在的死鎖問題。 假設(shè)你的代碼是這樣寫的:
import threading
x_lock = threading.Lock()
y_lock = threading.Lock()
def thread_1():
while True:
with acquire(x_lock):
with acquire(y_lock):
print('Thread-1')
def thread_2():
while True:
with acquire(y_lock):
with acquire(x_lock):
print('Thread-2')
t1 = threading.Thread(target=thread_1)
t1.daemon = True
t1.start()
t2 = threading.Thread(target=thread_2)
t2.daemon = True
t2.start()
如果你運行這個版本的代碼,必定會有一個線程發(fā)生崩潰,異常信息可能像這樣:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/local/lib/python3.3/threading.py", line 639, in _bootstrap_inner
self.run()
File "/usr/local/lib/python3.3/threading.py", line 596, in run
self._target(*self._args, **self._kwargs)
File "deadlock.py", line 49, in thread_1
with acquire(y_lock):
File "/usr/local/lib/python3.3/contextlib.py", line 48, in __enter__
return next(self.gen)
File "deadlock.py", line 15, in acquire
raise RuntimeError("Lock Order Violation")
RuntimeError: Lock Order Violation
>>>
發(fā)生崩潰的原因在于,每個線程都記錄著自己已經(jīng)獲取到的鎖。 acquire() 函數(shù)會檢查之前已經(jīng)獲取的鎖列表, 由于鎖是按照升序排列獲取的,所以函數(shù)會認(rèn)為之前已獲取的鎖的id必定小于新申請到的鎖,這時就會觸發(fā)異常。
討論
死鎖是每一個多線程程序都會面臨的一個問題(就像它是每一本操作系統(tǒng)課本的共同話題一樣)。根據(jù)經(jīng)驗來講,盡可能保證每一個 線程只能同時保持一個鎖,這樣程序就不會被死鎖問題所困擾。一旦有線程同時申請多個鎖,一切就不可預(yù)料了。
死鎖的檢測與恢復(fù)是一個幾乎沒有優(yōu)雅的解決方案的擴(kuò)展話題。一個比較常用的死鎖檢測與恢復(fù)的方案是引入看門狗計數(shù)器。當(dāng)線程正常 運行的時候會每隔一段時間重置計數(shù)器,在沒有發(fā)生死鎖的情況下,一切都正常進(jìn)行。一旦發(fā)生死鎖,由于無法重置計數(shù)器導(dǎo)致定時器 超時,這時程序會通過重啟自身恢復(fù)到正常狀態(tài)。
避免死鎖是另外一種解決死鎖問題的方式,在進(jìn)程獲取鎖的時候會嚴(yán)格按照對象id升序排列獲取,經(jīng)過數(shù)學(xué)證明,這樣保證程序不會進(jìn)入 死鎖狀態(tài)。證明就留給讀者作為練習(xí)了。避免死鎖的主要思想是,單純地按照對象id遞增的順序加鎖不會產(chǎn)生循環(huán)依賴,而循環(huán)依賴是 死鎖的一個必要條件,從而避免程序進(jìn)入死鎖狀態(tài)。
下面以一個關(guān)于線程死鎖的經(jīng)典問題:“哲學(xué)家就餐問題”,作為本節(jié)最后一個例子。題目是這樣的:五位哲學(xué)家圍坐在一張桌子前,每個人 面前有一碗飯和一只筷子。在這里每個哲學(xué)家可以看做是一個獨立的線程,而每只筷子可以看做是一個鎖。每個哲學(xué)家可以處在靜坐、 思考、吃飯三種狀態(tài)中的一個。需要注意的是,每個哲學(xué)家吃飯是需要兩只筷子的,這樣問題就來了:如果每個哲學(xué)家都拿起自己左邊的筷子, 那么他們五個都只能拿著一只筷子坐在那兒,直到餓死。此時他們就進(jìn)入了死鎖狀態(tài)。 下面是一個簡單的使用死鎖避免機(jī)制解決“哲學(xué)家就餐問題”的實現(xiàn):
import threading
# The philosopher thread
def philosopher(left, right):
while True:
with acquire(left,right):
print(threading.currentThread(), 'eating')
# The chopsticks (represented by locks)
NSTICKS = 5
chopsticks = [threading.Lock() for n in range(NSTICKS)]
# Create all of the philosophers
for n in range(NSTICKS):
t = threading.Thread(target=philosopher,
args=(chopsticks[n],chopsticks[(n+1) % NSTICKS]))
t.start()
最后,要特別注意到,為了避免死鎖,所有的加鎖操作必須使用 acquire() 函數(shù)。如果代碼中的某部分繞過acquire 函數(shù)直接申請鎖,那么整個死鎖避免機(jī)制就不起作用了。
相關(guān)文章
Django 接收Post請求數(shù)據(jù),并保存到數(shù)據(jù)庫的實現(xiàn)方法
今天小編就為大家分享一篇Django 接收Post請求數(shù)據(jù),并保存到數(shù)據(jù)庫的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
Python字符串編碼轉(zhuǎn)換 encode()和decode()方法詳細(xì)說明
這篇文章主要介紹了Python字符串編碼轉(zhuǎn)換 encode()和decode()方法詳細(xì)的說明,下面文章圍繞encode()和decode()方法的相相關(guān)資料展開內(nèi)容,具有一定的價值,需要的朋友卡通參考一下2021-12-12
教你用python3根據(jù)關(guān)鍵詞爬取百度百科的內(nèi)容
這篇文章介紹的是利用python3根據(jù)關(guān)鍵詞爬取百度百科的內(nèi)容,注意本文用的是python3版本以及根據(jù)關(guān)鍵詞爬取,爬取也只是單純的爬網(wǎng)頁信息,有需要的可以參考借鑒。2016-08-08
python tkinter圖形界面代碼統(tǒng)計工具
這篇文章主要為大家詳細(xì)介紹了python tkinter圖形界面代碼統(tǒng)計工具,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-09-09
python中WSGI是什么,Python應(yīng)用WSGI詳解
這篇文章主要介紹一下python中的WSGI, 小編在網(wǎng)上找了幾篇非常好的關(guān)于WSGI介紹,整理一下分享給大家。2017-11-11
Python Opencv使用ann神經(jīng)網(wǎng)絡(luò)識別手寫數(shù)字功能
這篇文章主要介紹了opencv(python)使用ann神經(jīng)網(wǎng)絡(luò)識別手寫數(shù)字,由于這里主要研究knn算法,為了圖簡單,直接使用Keras的mnist手寫數(shù)字解析模塊,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07

