首页 > 编程语言 > 详细

python线程与进程------生产者和消费者模式

时间:2019-08-02 16:07:51      阅读:92      评论:0      收藏:0      [点我收藏+]

import threading,queue
import time,random

q = queue.Queue()

def Producer(name):
    count =0
    while count < 10:
        print('making.........')
        time.sleep(random.randrange(3))
        q.put(count)
        print('Product %s has produced %s baozi...' % (name,count))
        count += 1
        q.task_done()
        print('ok.......')
        
def Consumer(name):
    count = 0
    while count < 10:
        time.sleep(random.randrange(4))
        if not q.empty():
            data = q.get()
            q.task_done
            q.join()
            print(data)
            print('Comsumer %s has eat %s baozi...' % (name,data))
        else:
            print("-----------------没有包子了-----------------")
        count += 1             

    
    
p1 = threading.Thread(target = Producer,args = ('厨师',))
c1 = threading.Thread(target = Consumer,args = ('食客1',)) 
c2 = threading.Thread(target = Consumer,args = ('食客2',))         
#c3 = threading.Thread(target = Consumer,args = ('食客3',))     
    
p1.start()
c1.start()
c2.start()    
#c3.start()    
    
    
    
        

python线程与进程------生产者和消费者模式

原文:https://www.cnblogs.com/hyxk/p/11289068.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!