队列queue实例(生产者和消费者模型)

import queue, threading, time

q = queue.Queue(maxsize=10)
def producter(n):
count = 1
while True:
q.put(count)
print('%s 生产了%s 骨头'%(n,count))
time.sleep(1)
count += 1

def consumer(n):
while True:
print('%s 获得了 %s 骨头'%(n,q.get()))
time.sleep(1)


p = threading.Thread(target=producter, args=('alex',))
c = threading.Thread(target=consumer, args=('陈荣华',))
c1 = threading.Thread(target=consumer, args=('王森', ))
p.start()
c.start()
c1.start()


猜你喜欢

转载自www.cnblogs.com/my-love-is-python/p/9134970.html