Python高级(14)—经典生产者消费者问题(多线程、多进程Condition版)

◎知识点

  1. 经典生产者消费者问题(多线程Condition版)

  2. 经典生产者消费者问题(多进程Condition版)


◎脚本练习

▽ 经典生产者消费者问题(多线程Condition版)

"""
    经典生产者消费者问题:
    假设有一群生产者(Producer)和一群消费者(Consumer)通过一个市场来交换产品。
    生产者的策略是:如果市场上剩余的产品少于20个,那么就生产4个产品放到市场上;
    消费者的策略是:如果市场上剩余的产品多于10个,那么就从市场上消费3个产品。
"""


from threading import Thread, Condition
import time

cond = Condition()
count = 0

class Producer(Thread):
    def run(self):
        global count, cond
        while True:
            cond.acquire()
            if count < 20:
                count += 4
                print('%s:生产者生产了4个产品,当前总共%d个产品' % (self.name, count))
                cond.notify()
            else:
                print('%s:不生产,等待' % self.name)
                cond.wait()
            cond.release()
            time.sleep(2)

class Consumer(Thread):
    def run(self):
        global count, cond
        while True:
            cond.acquire()
            if count > 10:
                count -= 3
                print('%s:消费者消费了3个产品,当前总共%d个产品' % (self.name, count))
                cond.notify()
            else:
                print('%s:不消费,等待' % self.name)
                cond.wait()
            cond.release()
            time.sleep(2)

for i in range(3):
    Producer().start()

for i in range(3):
    Consumer().start()


▽ 经典生产者消费者问题(多进程Condition版)

from multiprocessing import Process, Value, Condition
import time

cond = Condition()
count = Value('i', 0)

class Producer(Process):

    def run(self):
        global count, cond
        while True:
            cond.acquire()
            if count.value < 20:
                count.value += 4
                print('%s:生产者生产了4个产品,当前总共%d个产品' % (self.name, count.value))
                cond.notify()
            else:
                print('%s:不生产,等待' % self.name)
                cond.wait()
            cond.release()
            time.sleep(2)

class Consumer(Process):
    def run(self):
        global count, cond
        while True:
            cond.acquire()
            if count.value > 10:
                count.value -= 3
                print('%s:消费者消费了3个产品,当前总共%d个产品' % (self.name, count.value))
                cond.notify()
            else:
                print('%s:不消费,等待' % self.name)
                cond.wait()
            cond.release()
            time.sleep(2)

if __name__ == '__main__':

    for i in range(3):
        Producer().start()

    for i in range(3):
        Consumer().start()

Python高级(14)—经典生产者消费者问题(多线程、多进程Condition版)


◎脚本地址:https://github.com/anzhihe/learning/blob/master/python/practise/learn-python/python_senior/pc_condition.py

anzhihe 安志合个人博客,版权所有 丨 如未注明,均为原创 丨 转载请注明转自:https://chegva.com/5624.html | ☆★★每天进步一点点,加油!★★☆ | 

您可能还感兴趣的文章!

发表评论

电子邮件地址不会被公开。 必填项已用*标注