日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python wait方法_Python条件类| 带有示例的wait()方法

發布時間:2023/12/1 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python wait方法_Python条件类| 带有示例的wait()方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

python wait方法

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

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

wait()是Python中線程模塊的Condition類的內置方法。

Condition class implements condition variable objects. A condition variable allows one or more threads to wait until they are notified by another thread. wait() method is used to block the thread and wait until some other thread notifies it by calling the notify() or notify_all() method or if the timeout occurs. The method returns a True Boolean value if it is released by notify() or notify_all() method otherwise a timeout happens, in that case, it returns False. If you wait on an unacquired lock, a Runtime Error is raised.

條件類實現條件變量對象 。 條件變量允許一個或多個線程等待,直到被另一個線程通知為止。 wait()方法用于阻塞線程,并等待直到其他線程通過調用notify()或notify_all()方法通知該線程或發生超時。 如果方法是通過notify()或notify_all()方法釋放的,則該方法返回True布爾值,否則會發生超時,在這種情況下,它將返回False。 如果等待未獲得的鎖定,則會引發“運行時錯誤”。

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

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

Module:

模塊:

from threading import Condition

Syntax:

句法:

wait(timeout=None)

Parameter(s):

參數:

  • timeout: It is an optional parameter, which specifies the time for which the thread will wait for a notify call. Its default value is None.

    timeout :這是一個可選參數,它指定線程等待通知調用的時間。 其默認值為無。

Return value:

返回值:

The return type of this method is <class 'bool'>. It returns True if it gets notified by the notify() method within given time. In case of a timeout, it returns False.

此方法的返回類型為<class'bool'> 。 如果在指定時間內通過notify()方法得到通知,則返回True。 如果超時,則返回False。

Example:

例:

# Python program to explain the # use of wait() 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-wait-method-with-example.aspx

python wait方法

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。