日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

python 示例_Python条件类| release()方法与示例

發(fā)布時間:2025/3/11 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 示例_Python条件类| release()方法与示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

python 示例

Python Condition.release()方法 (Python Condition.release() Method)

release() is an inbuilt method of the Condition class of the threading module in Python.

release()是Python中線程模塊的Condition類的內(nèi)置方法。

Condition class implements condition variable objects. A condition variable allows one or more threads to wait until they are notified by another thread. release() method is used to release the lock which has been acquired by the calling thread in order to free the critical section of the code. Once the lock is released, it can be acquired by some other threads waiting for its turn to use the lock.

條件類實(shí)現(xiàn)條件變量對象 。 條件變量允許一個或多個線程等待,直到被另一個線程通知為止。 release()方法用于釋放由調(diào)用線程獲取的鎖,以釋放代碼的關(guān)鍵部分。 釋放鎖后,其他一些線程可以獲取該鎖,等待其輪換使用該鎖。

Read about Producer-Consumer here: Condition.acquire() Method

在這里閱讀有關(guān)Producer-Consumer的信息: Condition.acquire()方法

Module:

模塊:

from threading import Condition

Syntax:

句法:

release()

Parameter(s):

參數(shù):

  • None

    沒有

Return value:

返回值:

The return type of this method is <class 'NoneType'>. The method is used to release the underlying lock which is acquired by a given thread.

此方法的返回類型為<class'NoneType'> 。 該方法用于釋放由給定線程獲取的基礎(chǔ)鎖。

Example:

例:

# Python program to explain the # use of release() method for Condition objectimport threading import time import randomclass subclass:# Initialising the shared resourcesdef __init__(self):self.x = []# Add an item for the producerdef produce_item(self, x_item):print("Producer adding an item to the list")self.x.append(x_item)# Consume an item for the consumerdef 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 3r = random.randint(1,3)print("Random number selected was:", r)# Creting r number of items by the producerfor 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 itemsubclass_obj.produce_item(i)# Notify that an item has been producedcondition_obj.notify()finally:# Releasing the lock after producingcondition_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 secvalue = condition_obj.wait(10)if value:print("Item produced notified")continueelse:print("Waiting timeout")break# Releasig the lock after consumingcondition_obj.release()if __name__=='__main__':# Initialising a condition class objectcondition_obj = threading.Condition()# subclass objectsubclass_obj = subclass()# Producer threadpro = threading.Thread(target=producer, args=(subclass_obj,condition_obj,))pro.start()# consumer threadcon = threading.Thread(target=consumer, args=(subclass_obj,condition_obj,))con.start()pro.join()con.join()print("Producer Consumer code executed")

Output:

輸出:

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

翻譯自: https://www.includehelp.com/python/condition-release-method-with-example.aspx

python 示例

總結(jié)

以上是生活随笔為你收集整理的python 示例_Python条件类| release()方法与示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。