Python Lock.release() 方法
release() 是 Python 中線程模塊的 Lock 類的內置方法。
此方法釋放線程先前獲取的鎖。一旦鎖被釋放,它就可以被另一個線程獲取。這可以從任何線程調用,而不僅僅是從獲取它的線程調用。釋放未鎖定的鎖時,會引發 RuntimeError。
模塊:
from threading import Lock
用法:
release()
參數:
- None
 
返回值:
這個方法的返回類型是<class 'NoneType'>.它釋放獲得它的線程。
例:
# Python program to show
# the use of release() method in Lock class
import threading
import random
                    
class shared(object):
  
    def __init__(self, x = 0):
        # Created a Lock object
        self.lock = threading.Lock()
        self.incr = x
        
        # Increment function for the thread
    def incrementcounter(self):
        print("Waiting for the lock to be unlocked")
        # Lock acquired by the current thread
        self.lock.acquire()
        try:
            print('Lock acquired, current counter value:', self.incr)
            self.incr = self.incr + 1
        finally:
            print('Lock released, current counter value:', self.incr)
            # Lock released by the given thread
            self.lock.release()
def helper_thread(c):
    # Getting a random integer between 1 to 3
    r = random.randint(1,3)
    print("Random value selected:", r)
    for i in range(r):
      c.incrementcounter()
    print('Finished', str(threading.current_thread().getName()))
    print()
if __name__ == '__main__':
    obj = shared()
    thread1 = threading.Thread(target=helper_thread, args=(obj,))
    thread1.start()
    
    thread2 = threading.Thread(target=helper_thread, args=(obj,))
    thread2.start()
    thread1.join()
    thread2.join()
    
    print('Final counter value:', obj.incr)
輸出
Random value selected:2 Waiting for the lock to be unlocked Lock acquired, current counter value: 0 Lock released, current counter value: 1 Waiting for the lock to be unlocked Lock acquired, current counter value: 1 Lock released, current counter value: 2 Finished Thread-1 Random value selected:3 Waiting for the lock to be unlocked Lock acquired, current counter value: 2 Lock released, current counter value: 3 Waiting for the lock to be unlocked Lock acquired, current counter value: 3 Lock released, current counter value: 4 Waiting for the lock to be unlocked Lock acquired, current counter value: 4 Lock released, current counter value: 5 Finished Thread-2 Final counter value:5
相關用法
- Python Lock acquire()用法及代碼示例
 - Python Lock locked()用法及代碼示例
 - Python List remove()用法及代碼示例
 - Python List clear()用法及代碼示例
 - Python List pop()用法及代碼示例
 - Python List index()用法及代碼示例
 - Python List sort()用法及代碼示例
 - Python List count()用法及代碼示例
 - Python List reverse()用法及代碼示例
 - Python List copy()用法及代碼示例
 - Python List extend()用法及代碼示例
 - Python numpy.less()用法及代碼示例
 - Python Sympy Permutation.list()用法及代碼示例
 - Python Matplotlib.figure.Figure.subplots_adjust()用法及代碼示例
 - Python numpy.tril()用法及代碼示例
 - Python Matplotlib.pyplot.matshow()用法及代碼示例
 - Python __file__用法及代碼示例
 - Python Pandas Panel.add()用法及代碼示例
 - Python Matplotlib.axis.Tick.get_window_extent()用法及代碼示例
 - Python numpy.fromstring()用法及代碼示例
 
注:本文由純淨天空篩選整理自 Python Lock Class | release() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
