最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python中死鎖的形成示例及死鎖情況的防止

 更新時間:2016年06月14日 16:49:21   作者:mattkang  
由于Python中允許創(chuàng)建多個線程,那么互斥鎖或者線程同時獲取多個鎖的情況就有可能發(fā)生,這里我們就來看一下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() 

防止死鎖的加鎖機制
問題:
你正在寫一個多線程程序,其中線程需要一次獲取多個鎖,此時如何避免死鎖問題。
解決方案:
在多線程程序中,死鎖問題很大一部分是由于線程同時獲取多個鎖造成的。舉個例子:一個線程獲取了第一個鎖,然后在獲取第二個鎖的 時候發(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)鍵在于,在第一段代碼中,我們對這些鎖進行了排序。通過排序,使得不管用戶以什么樣的順序來請求鎖,這些鎖都會按照固定的順序被獲取。 如果有多個 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ù)會認為之前已獲取的鎖的id必定小于新申請到的鎖,這時就會觸發(fā)異常。

討論
死鎖是每一個多線程程序都會面臨的一個問題(就像它是每一本操作系統(tǒng)課本的共同話題一樣)。根據(jù)經(jīng)驗來講,盡可能保證每一個 線程只能同時保持一個鎖,這樣程序就不會被死鎖問題所困擾。一旦有線程同時申請多個鎖,一切就不可預(yù)料了。

死鎖的檢測與恢復(fù)是一個幾乎沒有優(yōu)雅的解決方案的擴展話題。一個比較常用的死鎖檢測與恢復(fù)的方案是引入看門狗計數(shù)器。當(dāng)線程正常 運行的時候會每隔一段時間重置計數(shù)器,在沒有發(fā)生死鎖的情況下,一切都正常進行。一旦發(fā)生死鎖,由于無法重置計數(shù)器導(dǎo)致定時器 超時,這時程序會通過重啟自身恢復(fù)到正常狀態(tài)。

避免死鎖是另外一種解決死鎖問題的方式,在進程獲取鎖的時候會嚴格按照對象id升序排列獲取,經(jīng)過數(shù)學(xué)證明,這樣保證程序不會進入 死鎖狀態(tài)。證明就留給讀者作為練習(xí)了。避免死鎖的主要思想是,單純地按照對象id遞增的順序加鎖不會產(chǎn)生循環(huán)依賴,而循環(huán)依賴是 死鎖的一個必要條件,從而避免程序進入死鎖狀態(tài)。

下面以一個關(guān)于線程死鎖的經(jīng)典問題:“哲學(xué)家就餐問題”,作為本節(jié)最后一個例子。題目是這樣的:五位哲學(xué)家圍坐在一張桌子前,每個人 面前有一碗飯和一只筷子。在這里每個哲學(xué)家可以看做是一個獨立的線程,而每只筷子可以看做是一個鎖。每個哲學(xué)家可以處在靜坐、 思考、吃飯三種狀態(tài)中的一個。需要注意的是,每個哲學(xué)家吃飯是需要兩只筷子的,這樣問題就來了:如果每個哲學(xué)家都拿起自己左邊的筷子, 那么他們五個都只能拿著一只筷子坐在那兒,直到餓死。此時他們就進入了死鎖狀態(tài)。 下面是一個簡單的使用死鎖避免機制解決“哲學(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ù)直接申請鎖,那么整個死鎖避免機制就不起作用了。

相關(guān)文章

最新評論

金湖县| 建湖县| 保靖县| 咸丰县| 临泽县| 青铜峡市| 陕西省| 诸暨市| 调兵山市| 易门县| 富民县| 浦县| 社旗县| 电白县| 江油市| 洪江市| 建阳市| 寿宁县| 友谊县| 桐城市| 多伦县| 和田市| 昔阳县| 宿州市| 故城县| 车致| 德州市| 开封县| 大新县| 大安市| 康马县| 江都市| 南投市| 福贡县| 太白县| 玉屏| 宁夏| 汝阳县| 镶黄旗| 柘城县| 泸西县|