生产消费者模型

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()

 

posted @ 2018-05-07 07:34  阜阳小全  阅读(98)  评论(0编辑  收藏  举报