#coding:utf-8 #!/usr/bin/env python from Queue import Queue from threading import Thread import time class Procuder(Thread): def __init__(self,name,queue): self.__Name = name self.__Queue = queue super(Procuder,self).__init__() def run(self): while True: if self.__Queue.full(): time.sleep(1) else: self.__Queue.put(‘baozi‘) print ‘%s 生产了一个包子...‘ % (self.__Name,) Thread.run(self) class Consumer(Thread): def __init__(self,name,queue): self.__Name = name self.__Queue = queue super(Consumer,self).__init__() def run(self): while True: if self.__Queue.empty(): time.sleep(1) else: self.__Queue.get(‘baozi‘) print ‘%s 消费了一个包子...‘ % (self.__Name,) Thread.run(self) que = Queue(maxsize=100) #Three producers laogou = Procuder(‘laogou‘,que) laogou.start() laogou2 = Procuder(‘laogou2‘,que) laogou2.start() laogou3 = Procuder(‘laogou3‘,que) laogou3.start() #20 consumers for items in range(20): name = ‘tantianran%d‘ % (items,) temp = Consumer(name,que) temp.start()
本文出自 “运维交流Q群:223843163” 博客,请务必保留此出处http://freshair.blog.51cto.com/8272891/1897219
原文:http://freshair.blog.51cto.com/8272891/1897219