python wait之后怎么起起来_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 . It returns True if it gets notified by the notify() method within given time. In case of a timeout, it returns False.

此方法的返回类型为。 如果在指定时间内通过notify()方法得到通知,则返回True。 如果超时,则返回False。

Example:

例:

# Python program to explain the

# use of wait() 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")

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方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值