self.data=http://developer.51cto.com/art/201705/queue def run(self): for i in range(5): val = self.data.get() print("%s is consuming. %d in the queue is consumed!" % (self.getName(),val)) time.sleep(random.randrange(10)) print("%s finished!" % self.getName()) def main(): queue = Queue() producer = Producer('Producer',queue) consumer = Consumer('Consumer',queue) producer.start() consumer.start() producer.join() consumer.join() print 'All threads finished!' if __name__ == '__main__': main() 履行结不雅可能如下:
- Producer is producing 0 to the queue!
-
- Consumer is consuming. 0 in the queue is consumed!
-
- Producer is producing 1 to the queue!
-
- Producer is producing 2 to the queue!
-
- Consumer is consuming. 1 in the queue is consumed!
-
- Consumer is consuming. 2 in the queue is consumed!
-
- Producer is producing 3
推荐阅读
如何使用VCHA创建集群保护负载
VCHA(VCenter Server High Availability)应用一个包含三个节点——active、passive和witness——的集群来尽量避免体系故障可能带来的影响。如不雅治理员想要应用 vC>>>详细阅读
本文标题:用Python多线程实现生产者消费者模式
地址:http://www.17bianji.com/lsqh/35243.html
1/2 1