Python3生产者/消费者模式

import threading  
import queue,time,random  
  
class Goods:#产品类  
    def __init__(self):  
        self.count = 0
    def add(self,num = 1):
        self.count += num  
    def sub(self):  
        if self.count>=0:  
            self.count -= 1  
    def empty(self):  
        return self.count <= 0

class Producer(threading.Thread):#生产者类  
    def __init__(self,condition,goods,sleeptime = 1):#sleeptime=1  
        threading.Thread.__init__(self)  
        self.cond = condition  
        self.goods = goods  
        self.sleeptime = sleeptime  
    def run(self):  
        cond = self.cond  
        goods = self.goods  
        while True:
            cond.acquire()#锁住资源  
            goods.add()
            print("产品数量:",goods.count,"生产者线程")
            cond.notifyAll()#唤醒所有等待的线程--》其实就是唤醒消费者进程 
            cond.release()#解锁资源
            time.sleep(self.sleeptime)

class Consumer(threading.Thread):#消费者类  
    def __init__(self,condition,goods,sleeptime = 2):#sleeptime=2  
        threading.Thread.__init__(self)
        self.cond = condition
        self.goods = goods
        self.sleeptime = sleeptime
    def run(self):  
        cond = self.cond
        goods = self.goods
        while True:
            time.sleep(self.sleeptime)
            cond.acquire()#锁住资源
            while goods.empty():#如无产品则让线程等待
                cond.wait()
            goods.sub()
            print("产品数量:",goods.count,"消费者线程")
            cond.release()#解锁资源  

g = Goods()
c = threading.Condition()
  
pro = Producer(c,g)  
pro.start()  
  
con = Consumer(c,g)  
con.start()  

Python的锁

import threading
import time

private_lock = threading.RLock()
num = 0


class MyThread(threading.Thread):
    def __init__(self, name):
        threading.Thread.__init__(self)
        self.t_name = name 
        
    def run(self):
        global num
        while True:
            private_lock.acquire()
            if num > 4:
                print("%s" % self.t_name)
                time.sleep(5)
                private_lock.release()
                break
            num += 1
            private_lock.release()


def test1():
    thread1 = MyThread('A')
    thread2 = MyThread('B')
    thread1.start()
    thread2.start()


if __name__ == '__main__':
    test1()

走路放慢脚步
107 声望3 粉丝

It is what you do that defines you


引用和评论

0 条评论