python-queue队列-生产者消费者
import threading,time import queue q = queue.Queue(maxsize=10) def Producer(name):#生产者 count=1 while True: q.put("骨头%s"%count) print("生产了骨头",count) count +=1 time.sleep(2) def Consumer(name):#消费者 while True: time.sleep(1) print("[%s] 取到[%s] 并且吃了它。。。"%(name,q.get())) p =threading.Thread(target=Producer,args=("Alex",)) c =threading.Thread(target=Consumer,args=("goupang",)) c1 =threading.Thread(target=Consumer,args=("baipang",)) p.start() c.start() c1.start()
队列最主要的两个作用:
1.解耦(松耦合)
2.提高效率