生产消费者模型

import time,random
import queue,threading

q = queue.Queue()

def Producer(name):
    count = 0
    while count < 10:
        print('making-------------')
        time.sleep(random.randrange(3))
        q.put(count)
        print('生产者 %s 正在生产 %s 号包子  ' %(name,count))
        count += 1

def Customer(name):

    while True:
        count = 0
        time.sleep(random.randrange(5))
        if not q.empty():
            q.get()
            print('消费者 %s 正在吃%s 号包子' %(name,q.get()))

        else:
            print('-------------没有包子了----------------')

        count += 1


p1 = threading.Thread(target=Producer,args='P')
c2 = threading.Thread(target=Customer,args='C')

p1.start()
c2.start()

猜你喜欢

转载自www.cnblogs.com/lhqlhq/p/9000786.html
今日推荐