我们将模拟一条有 3个固定工位 的披萨生产线
| 工位 | 职责 |
| Station 1 | 铺面团(Dough) |
| Station 2 | 涂酱料(Sauce) |
| Station 3 | 加配料(Topping) |
披萨从 Station 1 → 2 → 3 流动,每个工位独立工作。
我们来模拟一次性提交 5 个披萨订单,使用带输入队列的三工位流水线模型。
这样可以清晰看到:
- 前 3 个订单迅速填满三个工位,
- 第 4、5 个订单在输入队列中等待,
- 随着流水线推进,新订单逐步进入工位1,
- 始终保证每个工位同时处理不同披萨。
from collections import deque
class Pizza:
def __init__(self, order_id, dough=None, sauce=None, topping=None):
self.order_id = order_id
self.dough = dough
self.sauce = sauce
self.topping = topping
def __str__(self):
return f"Pizza#{self.order_id}(dough={self.dough}, sauce={self.sauce}, topping={self.topping})"
class PizzaAssemblyLine:
def __init__(self):
self.input_queue = deque() # 输入缓冲区(排队区)
self.station1_pizza = None # 工位1:铺面团
self.station2_pizza = None # 工位2:涂酱料
self.station3_pizza = None # 工位3:加配料
self.completed = []
self.next_order_id = 1
def receive_order(self, dough, sauce, topping):
"""接收订单:加入输入队列"""
order_id = self.next_order_id
self.input_queue.append((order_id, dough, sauce, topping))
print(f"📥 订单 #{order_id} 已提交: 面团={dough}, 酱={sauce}, 配料={topping}")
self.next_order_id += 1
def _load_station1(self):
"""若工位1空闲且队列非空,则加载新订单"""
if self.station1_pizza is None and self.input_queue:
order_id, dough, sauce, topping = self.input_queue.popleft()
pizza = Pizza(order_id, dough=dough, sauce=sauce, topping=topping)
self.station1_pizza = pizza
print(f" ➡️ 订单 #{order_id} 进入工位1(铺面团)")
def step(self):
"""推进一个生产周期:所有工位同步工作"""
print("\n🔄 流水线推进一个周期...")
# 工位3 → 完成
if self.station3_pizza is not None:
p = self.station3_pizza
print(f" 🍄 工位3: Pizza#{p.order_id} 加配料 '{p.topping}' → 完成!")
self.completed.append(p)
self.station3_pizza = None
# 工位2 → 工位3
if self.station2_pizza is not None:
p = self.station2_pizza
print(f" 🍅 工位2: Pizza#{p.order_id} 涂酱 '{p.sauce}' → 移交工位3")
self.station3_pizza = p
self.station2_pizza = None
# 工位1 → 工位2
if self.station1_pizza is not None:
p = self.station1_pizza
print(f" 🥖 工位1: Pizza#{p.order_id} 铺面团 '{p.dough}' → 移交工位2")
self.station2_pizza = p
self.station1_pizza = None
# 尝试加载新订单到工位1
self._load_station1()
# 显示当前队列状态(可选)
if self.input_queue:
waiting = [f"#{oid}" for oid, _, _, _ in self.input_queue]
print(f" ⏳ 等待队列: {', '.join(waiting)}")
def has_work(self):
return (len(self.input_queue) > 0 or
self.station1_pizza is not None or
self.station2_pizza is not None or
self.station3_pizza is not None)
def deliver_completed(self):
delivered = self.completed[:]
self.completed.clear()
return delivered
# ========================
# 🏭 一次性提交 5 个订单
# ========================
if __name__ == "__main__":
line = PizzaAssemblyLine()
print("🏭 启动三工位披萨流水线(带输入缓冲区)\n")
# 一次性提交 5 个不同订单
orders = [
("thin", "tomato", "mushrooms"),
("thick", "bbq", "chicken"),
("thin", "pesto", "olives"),
("stuffed", "tomato", "pepperoni"),
("gluten-free", "white", "spinach, ricotta")
]
for dough, sauce, topping in orders:
line.receive_order(dough, sauce, topping)
print(f"\n🚀 开始生产!共 {len(orders)} 个订单,队列已满...\n")
# 持续运行直到所有订单完成
cycle = 0
while line.has_work():
cycle += 1
print(f"--- 第 {cycle} 个生产周期 ---")
line.step()
# 交付所有完成品
print("\n📦 所有披萨已完成,正在交付...")
for pizza in line.deliver_completed():
print(f"✅ {pizza}")
print("\n🔚 生产结束。流水线空闲。")
26万+

被折叠的 条评论
为什么被折叠?



