Python多线程生产者消费者模式 发表于 2015-07-02 | 分类于 技术 | | 阅读次数: | 字数统计: 202 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748from Queue import Queueimport threadingimport randomimport timeclass ProduceToQueue( threading.Thread ): def __init__( self, threadName, queue ): threading.Thread.__init__( self, name = threadName ) self.sharedObject = queue def run( self ): for i in range( 11, 21 ): time.sleep( random.randrange( 4 ) ) print "%s adding %s to queue" % ( self.getName(), i ) self.sharedObject.put( i ) print self.getName(), "finished producing values" print "Terminating", self.getName()class ConsumeFromQueue( threading.Thread ): def __init__( self, threadName, queue ): threading.Thread.__init__( self, name = threadName ) self.sharedObject = queue def run( self ): sum = 0 current = 10 for i in range( 10 ): time.sleep( random.randrange( 4 ) ) print "%s attempting to read %s..." % ( self.getName(), current + 1 ) current = self.sharedObject.get() print "%s read %s" % ( self.getName(), current ) sum += current print "%s retrieved values totaling: %d" % ( self.getName(), sum ) print "Terminating", self.getName()queue = Queue()producer = ProduceToQueue( "Producer", queue )consumer = ConsumeFromQueue( "Consumer", queue )producer.start()consumer.start()producer.join()consumer.join()