消息队列-queue
from threading import Thread
from queue import Queue
import random,time
储钱罐
def create(queue):
for i in [100,50,20,10,5,1,0.5]:
if not queue.full():
queue.put(i) # 入队列
print("Put %sRMB to queue." %i)
time.sleep(1)
取储钱罐中的零钱花
def get(queue):
while 1:
if not queue.empty():
print("Get %sRMB from queue." %queue.get())
time.sleep(2)
else:
break
q = Queue(5) # 创建一个最大长度为5的队列实例
create_t=Thread(target=create,args=(q,))
get_t=Thread(target=get,args=(q,))
create_t.start()
get_t.start()
create_t.join()
get_t.join()