Python Condition.acquire() 方法
acquire() 是 Python 中線程模塊的 Condition 類的內置方法。
條件類實現條件變量對象。條件變量允許一個或多個線程等待,直到它們被另一個線程通知。 Acquire 方法用於獲取鎖。它調用底層鎖上的相應方法並返回方法值。
Producer-Consumer 問題是多線程中 Condition 對象的完美用例。在這個問題中,有一個生產者生產某種物品,一個消費者消費該物品。在生產者還沒有生產物品之前,消費者不能消費它。因此,它等待 Producer 生產項目。此外,生產者有責任通知消費者某物品已生產並可供消費。如果有多個消費者,那麽生產者必須將新項目通知所有消費者。
模塊:
from threading import Condition
用法:
acquire(*args)
參數:
args
: 它是一個可選參數,指定我們可以放入 acquire() 方法的參數。
返回值:
這個方法的返回類型是<class 'bool'>
.該方法用於在實現多線程時獲取鎖,如果成功獲取鎖則返回 True,否則返回 False。
例:
# use of acquire() method for Condition object
import threading
import time
import random
class subclass:
# Initialising the shared resources
def __init__(self):
self.x = []
# Add an item for the producer
def produce_item(self, x_item):
print("Producer adding an item to the list")
self.x.append(x_item)
# Consume an item for the consumer
def consume_item(self):
print("Consuming from the list")
consumed_item = self.x[0]
print("Consumed item:", consumed_item)
self.x.remove(consumed_item)
def producer(subclass_obj, condition_obj):
# Selecting a random number from the 1 to 3
r = random.randint(1,3)
print("Random number selected was:", r)
# Creting r number of items by the producer
for i in range(1, r):
print("Producing an item, time it will take(seconds):" + str(i))
time.sleep(i)
print("Producer acquiring the lock")
condition_obj.acquire()
try:
# Produce an item
subclass_obj.produce_item(i)
# Notify that an item has been produced
condition_obj.notify()
finally:
# Releasing the lock after producing
condition_obj.release()
def consumer(subclass_obj, condition_obj):
condition_obj.acquire()
while True:
try:
# Consume the item
subclass_obj.consume_item()
except:
print("No item to consume, list empty")
print("Waiting for 10 seconds")
# wait with a maximum timeout of 10 sec
value = condition_obj.wait(10)
if value:
print("Item produced notified")
continue
else:
print("Waiting timeout")
break
# Releasig the lock after consuming
condition_obj.release()
if __name__=='__main__':
# Initialising a condition class object
condition_obj = threading.Condition()
# subclass object
subclass_obj = subclass()
# Producer thread
pro = threading.Thread(target=producer, args=(subclass_obj,condition_obj,))
pro.start()
# consumer thread
con = threading.Thread(target=consumer, args=(subclass_obj,condition_obj,))
con.start()
pro.join()
con.join()
print("Producer Consumer code executed")
輸出:
Random number selected was:3 Producing an item, time it will take(seconds):1 Consuming from the list No item to consume, list empty Waiting for 10 seconds Producer acquiring the lock Producer adding an item to the list Item produced notified Consuming from the list Consumed item: 1 Consuming from the list No item to consume, list empty Waiting for 10 seconds Producing an item, time it will take(seconds):2 Producer acquiring the lock Producer adding an item to the list Item produced notified Consuming from the list Consumed item: 2 Consuming from the list No item to consume, list empty Waiting for 10 seconds Waiting timeout Producer Consumer code executed
相關用法
- Python Condition release()用法及代碼示例
- Python Condition notify()用法及代碼示例
- Python Condition wait()用法及代碼示例
- Python Condition notify_all()用法及代碼示例
- Python Collections.UserString用法及代碼示例
- Python Collections.UserDict用法及代碼示例
- Python Collections.UserList用法及代碼示例
- Python Calendar itermonthdays2()用法及代碼示例
- Python Calendar monthdatescalendar()用法及代碼示例
- Python Calendar itermonthdates()用法及代碼示例
- Python Calendar iterweekdays()用法及代碼示例
- Python Calendar monthdayscalendar()用法及代碼示例
- Python Calendar itermonthdays3()用法及代碼示例
- Python Calendar itermonthdays4()用法及代碼示例
- Python Sympy Curve.translate()用法及代碼示例
- Python Calendar yeardatescalendar()用法及代碼示例
- Python Calendar itermonthdays()用法及代碼示例
- Python Calendar yeardayscalendar()用法及代碼示例
- Python Calendar yeardays2calendar()用法及代碼示例
- Python Calendar monthdays2calendar()用法及代碼示例
注:本文由純淨天空篩選整理自 Python Condition Class | acquire() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。