notify和notify
Python Condition.notify()方法 (Python Condition.notify() Method)
notify() is an inbuilt method of the Condition class of the threading module in Python.
notify()是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. This method is used to wake any thread which is waiting on a condition variable. The thread must be called only when the lock has been acquired by the calling thread. This method can be used to wakes up at most n of the threads that are waiting for the condition variable; it is a no-op if no threads are waiting.
条件类实现条件变量对象 。 条件变量允许一个或多个线程等待,直到被另一个线程通知为止。 此方法用于唤醒任何正在等待条件变量的线程。 仅当调用线程已获取锁时,才必须调用该线程。 此方法可用于唤醒最多n个正在等待条件变量的线程。 如果没有线程在等待,则为空操作。
Read about Producer-Consumer here: Condition.acquire() Method
在这里阅读有关Producer-Consumer的信息: Condition.acquire()方法
Module:
模块:
from threading import Condition
Syntax:
句法:
notify(n=1)
Parameter(s):
参数:
n: It is an optional parameter, which specifies the number of threads that can waken up which were waiting on the condition variable. Its default value is 1.
n :这是一个可选参数,它指定在条件变量上等待的可以唤醒的线程数。 默认值为1。
Return value:
返回值:
The return type of this method is <class 'NoneType'>. The method does not return anything. It notifies the threads that are waiting on the condition.
此方法的返回类型为<class'NoneType'> 。 该方法不返回任何内容。 它通知正在等待条件的线程。
Example:
例:
# Python program to explain the
# use of notify() 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-notify-method-with-example.aspx
notify和notify