# 队列 list
import queue
import random
import time
import threading
orders_q = queue.Queue()#队列实例化
#生产者/消费者(多个消费者)模式
def producer():#一个生产者
for i in range(10):
order_id = random.randint(1, 99999)
print('订单生成,orderid:%s'%order_id)
orders_q.put(order_id)
time.sleep(1)
def consumer():#消费者1
while True:
if orders_q.qsize()>0:
oreder_id = orders_q.get()
print('consumer1,订单落库',oreder_id)
def consumer2():#消费者2
while True:
if orders_q.qsize()>0:
oreder_id = orders_q.get()
print('consumer2,订单落库',oreder_id)
t = threading.Thread(target=producer)
t.start()
t = threading.Thread(target=consumer)
t.start()
t = threading.Thread(target=consumer2)
t.start()
python 队列
最新推荐文章于 2023-04-23 09:51:32 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)