当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python Condition notify_all()用法及代码示例


Python Condition.notify_all() 方法

notify_all() 是 Python 中线程模块的 Condition 类的内置方法。

条件类实现条件变量对象。条件变量允许一个或多个线程等待,直到它们被另一个线程通知。此方法用于唤醒所有正在等待条件变量的线程。

在此处阅读有关 Producer-Consumer 的信息:Condition.acquire() 方法

模块:

    from threading import Condition

用法:

    notify_all()

参数:

  • None

返回值:

这个方法的返回类型是<class 'NoneType'>.该方法不返回任何内容。它通知所有正在等待该条件的线程。

例:

# Python program to explain the
# 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)
    print("Consumed by:", str(threading.current_thread().getName()))
    self.x.remove(consumed_item)

def producer(subclass_obj, condition_obj):
    # Selecting a random number from the 1 to 4
    r = random.randint(1,4)
    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 all consumers that an item  has been produced
        condition_obj.notify_all()
      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()
  
  # Consumer1 thread
  con1 = threading.Thread(target=consumer, args=(subclass_obj,condition_obj,))
  con1.start()

  # Consumer2 thread
  con2 = threading.Thread(target=consumer, args=(subclass_obj,condition_obj,))
  con2.start()

  pro.join()
  con1.join()
  con2.join()
  print("Producer Consumer code executed")

输出:

Random number selected was:4
Producing an item, time it will take(seconds):1
Consuming from the list
No item to consume, list empty
Waiting for 10 seconds
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
Consumed by:Thread-2
Consuming from the list
No item to consume, list empty
Waiting for 10 seconds
Item produced notified
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
Consumed by:Thread-3
Consuming from the list
No item to consume, list empty
Waiting for 10 seconds
Producing an item, time it will take(seconds):3Item produced notified

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: 3
Consumed by:Thread-3
Consuming from the list
No item to consume, list empty
Waiting for 10 seconds
Item produced notified
Consuming from the list
No item to consume, list empty
Waiting for 10 seconds
Waiting timeout
Waiting timeout
Producer Consumer code executed


相关用法


注:本文由纯净天空筛选整理自 Python Condition Class | notify_all() Method with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。