Thread Based Parallelism

  1 Thread Based Parallelism - Thread Synchronization With a Condition
  2 
  3     from threading import Thread, Condition
  4     import time
  5 
  6     items = []
  7     condition = Condition()
  8 
  9     class consumer(Thread):
 10         def __init__(self):
 11             Thread.__init__(self)
 12 
 13         def consume(self):
 14             global condition
 15             global items
 16 
 17             condition.acquire()
 18             if len(items) == 0:
 19                 condition.wait()
 20                 print("Consumer notify : no item to consume")
 21             items.pop()
 22             print("Consumer notify : consumed 1 item")
 23             print("Consumer notify : items to consume are : " + str(len(items)))
 24             if len(items) == 0:
 25                 print("Consumer notify : no items to consume in future")
 26             condition.notify()
 27             condition.release()
 28 
 29         def run(self):
 30             for i in range(0, 10):
 31                 time.sleep(2)
 32                 self.consume()
 33 
 34     class producer(Thread):
 35         def __init__(self):
 36             Thread.__init__(self)
 37 
 38         def produce(self):
 39             global condition
 40             global items
 41 
 42             condition.acquire()
 43             if len(items) == 4:
 44                 condition.wait()
 45                 print("Producer notify : items producted are "  + str(len(items)))
 46                 print("Producer notify : stop the production!!")
 47             items.append(1)
 48             print("Producer notify : total items producted " + str(len(items)))
 49             condition.notify()
 50             condition.release()
 51 
 52         def run(self):
 53             for i in range(0, 10):
 54                 time.sleep(1)
 55                 self.produce()
 56 
 57     if __name__ == "__main__":
 58         producer = producer()
 59         consumer = consumer()
 60 
 61         producer.start()
 62         consumer.start()
 63 
 64         producer.join()
 65         consumer.join()
 66 
 67 Output,
 68     Producer notify : total items producted 1
 69     Consumer notify : consumed 1 item
 70     Consumer notify : items to consume are : 0
 71     Consumer notify : no items to consume in future
 72     Producer notify : total items producted 1
 73     Producer notify : total items producted 2
 74     Consumer notify : consumed 1 item
 75     Consumer notify : items to consume are : 1
 76     Producer notify : total items producted 2
 77     Producer notify : total items producted 3
 78     Consumer notify : consumed 1 item
 79     Consumer notify : items to consume are : 2
 80     Producer notify : total items producted 3
 81     Producer notify : total items producted 4
 82     Consumer notify : consumed 1 item
 83     Consumer notify : items to consume are : 3
 84     Producer notify : total items producted 4
 85     Consumer notify : consumed 1 item
 86     Consumer notify : items to consume are : 3
 87     Producer notify : items producted are 3
 88     Producer notify : stop the production!!
 89     Producer notify : total items producted 4
 90     Consumer notify : consumed 1 item
 91     Consumer notify : items to consume are : 3
 92     Producer notify : items producted are 3
 93     Producer notify : stop the production!!
 94     Producer notify : total items producted 4
 95     Consumer notify : consumed 1 item
 96     Consumer notify : items to consume are : 3
 97     Consumer notify : consumed 1 item
 98     Consumer notify : items to consume are : 2
 99     Consumer notify : consumed 1 item
100     Consumer notify : items to consume are : 1
101     Consumer notify : consumed 1 item
102     Consumer notify : items to consume are : 0
103     Consumer notify : no items to consume in future
原文地址:https://www.cnblogs.com/zzyzz/p/8431931.html