项目结构:

业务流程天然适配:1 个订单生成任务,会同时触发「原料采购核验、设计制图、加工生产、质检、包装、物流」6 个并行工序,完美体现扇出模式。
珠宝全业务流程:
生产者:客户下单系统(生成珠宝订单任务)
扇出分发:将订单同步分发给 6 个独立工作器
工作器(Worker):
原料核验:检查金 / 钻石 / 宝石库存与资质
设计制图:生成 3D 设计图
加工生产:金工铸造、宝石镶嵌
品质质检:成色、纯度、工艺检测
礼盒包装:定制包装、证书装订
物流发货:生成快递单、安排配送

# encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述: Fan-Out Pattern Fan-Out 模式 扇出模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2024.3.6 python 3.11
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/6/20 8:52
# User      :  geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : settings.py
"""
配置层
全局配置文件
企业级:所有硬编码统一管理
"""
import queue
 
# 任务队列配置
TASK_QUEUE_MAX_SIZE = 100
TASK_QUEUE = queue.Queue(maxsize=TASK_QUEUE_MAX_SIZE)
 
# 业务配置
ORDER_SLEEP_TIME = 1  # 下单间隔
 
 
 
# encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述: Fan-Out Pattern Fan-Out 模式 扇出模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2024.3.6 python 3.11
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/6/20 8:53
# User      :  geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : order.py
"""
模型层:类型安全
订单数据模型
使用 dataclass 保证类型约束,企业级规范
"""
from dataclasses import dataclass
 
@dataclass
class JewelryOrder:
    """
    实体
    """
    order_id: str
    product: str
    material: str
    style: str
 
 
 
 
# encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述: Fan-Out Pattern Fan-Out 模式 扇出模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2024.3.6 python 3.11
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/6/20 8:54
# User      :  geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : business_exc.py
"""
异常层
业务异常定义
"""
class JewelryBusinessException(Exception):
    """
    珠宝业务基础异常
    """
    pass
 
class WorkerExecuteException(JewelryBusinessException):
    """
    工作器执行异常
    """
    pass


# encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述: Fan-Out Pattern Fan-Out 模式 扇出模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2024.3.6 python 3.11
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/6/20 8:56
# User      :  geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : material.py
 
import time
from FanOutPattern.models.order import JewelryOrder
 
def execute(order: JewelryOrder):
    """
    原料核验工作器
    :param order:
    :return:
    """
    print(f"[原料核验] 订单{order.order_id}:检查{order.material}库存、GIA证书")
    time.sleep(1)
    print(f"[原料核验] 订单{order.order_id}:核验完成 ✅")
 
 
 
# encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Fan-Out Pattern Fan-Out 模式 扇出模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2024.3.6 python 3.11
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/6/20 8:56
# User      :  geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : design.py
import time
from FanOutPattern.models.order import JewelryOrder
 
def execute(order: JewelryOrder):
    """
     珠宝设计工作器
    :param order:
    :return:
    """
    print(f"[设计制图] 订单{order.order_id}:生成{order.style}3D图")
    time.sleep(1.5)
    print(f"[设计制图] 订单{order.order_id}:设计定稿 ✅")
 
 
 
# encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Fan-Out Pattern Fan-Out 模式 扇出模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2024.3.6 python 3.11
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/6/20 8:58
# User      :  geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : production.py
import time
from FanOutPattern.models.order import JewelryOrder
 
def execute(order: JewelryOrder):
    """
     QC工作器
    :param order:
    :return:
    """
    print(f"[加工生产] 订单{order.order_id}:{order.product}铸造镶嵌")
    time.sleep(2)
    print(f"[加工生产] 订单{order.order_id}:生产完成 ✅")
 
 
 
# encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Fan-Out Pattern Fan-Out 模式 扇出模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2024.3.6 python 3.11
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/6/20 8:59
# User      :  geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : quality.py
import time
from FanOutPattern.models.order import JewelryOrder
 
def execute(order: JewelryOrder):
    """
    QC工作器
    :param order:
    :return:
    """
    print(f"[品质质检] 订单{order.order_id}:纯度/净度/工艺检测")
    time.sleep(1)
    print(f"[品质质检] 订单{order.order_id}:质检合格 ✅")
 
 
 
# encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Fan-Out Pattern Fan-Out 模式 扇出模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2024.3.6 python 3.11
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/6/20 9:00
# User      :  geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : packaging.py
import time
from FanOutPattern.models.order import JewelryOrder
 
def execute(order: JewelryOrder):
    """
    编货工作器
    :param order:
    :return:
    """
    print(f"[礼盒包装] 订单{order.order_id}:定制礼盒+证书装订")
    time.sleep(0.8)
    print(f"[礼盒包装] 订单{order.order_id}:包装完成 ✅")
 
 
# encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Fan-Out Pattern Fan-Out 模式 扇出模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2024.3.6 python 3.11
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/6/20 9:00
# User      :  geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : logistics.py
import time
from FanOutPattern.models.order import JewelryOrder
 
def execute(order: JewelryOrder):
    """
    物流工作器
    :param order:
    :return:
    """
    print(f"[物流发货] 订单{order.order_id}:生成顺丰保价快递")
    time.sleep(0.5)
    print(f"[物流发货] 订单{order.order_id}:已发货 ✅")


# encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Fan-Out Pattern Fan-Out 模式 扇出模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2024.3.6 python 3.11
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/6/20 9:05
# User      :  geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : producer.py
"""
任务生产者:只负责生成订单
"""
import time
from FanOutPattern.config.settings import TASK_QUEUE, ORDER_SLEEP_TIME
from FanOutPattern.models.order import JewelryOrder
 
class OrderProducer:
    """
    生产者:单一职责
    """
 
    def __init__(self):
        self.orders = [
            JewelryOrder("J2025001", "18K金钻石戒指", "18K金+50分钻石", "经典六爪"),
            JewelryOrder("J2025002", "翡翠玉镯", "A货翡翠", "复古圆条"),
            JewelryOrder("J2025003", "铂金项链", "PT950铂金", "简约锁骨链")
        ]
 
    def produce(self):
        """
        生产订单并推入队列
        :return:
        """
        print("\n===== 客户订单开始生成 =====")
        for order in self.orders:
            print(f"📝 新订单:{order.product} | {order.order_id}")
            TASK_QUEUE.put(order)
            time.sleep(ORDER_SLEEP_TIME)
 
        # 结束信号
        TASK_QUEUE.put(None)
        print("===== 所有订单生成完成 =====")
 
 
 
 
# encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Fan-Out Pattern Fan-Out 模式 扇出模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2024.3.6 python 3.11
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/6/20 9:06
# User      :  geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : dispatcher.py
"""
Fan-Out 分发核心
Fan-Out 分发器:只负责任务分发
企业级:解耦、可复用、可测试
"""
import threading
from FanOutPattern.config.settings import TASK_QUEUE
from FanOutPattern.models.order import JewelryOrder
from FanOutPattern.workers import WORKER_LIST
 
class FanOutDispatcher:
    """
    Fan-Out 分发核心
    """
    def __init__(self):
        self.workers = WORKER_LIST
 
    def _execute_worker_parallel(self, order: JewelryOrder):
        """
        扇出:并行执行所有工作器
        :param order:
        :return:
        """
        threads = []
        for worker in self.workers:
            t = threading.Thread(target=worker, args=(order,))
            threads.append(t)
            t.start()
 
        for t in threads:
            t.join()
 
    def start(self):
        """
        启动分发器监听队列
        :return:
        """
        while True:
            order = TASK_QUEUE.get()
            if order is None:
                break
 
            print(f"\n===== 订单{order.order_id} 扇出分发启动 =====")
            self._execute_worker_parallel(order)
            print(f"===== 订单{order.order_id} 全流程完成 =====\n")
            TASK_QUEUE.task_done()
  

调用:

# encoding: utf-8
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2024.3.6 python 3.11
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/6/20 9:08
# User      :  geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : FanOutBll.py
 
"""
项目启动
企业级:简洁、清晰、唯一入口
"""
import threading
from FanOutPattern.core.producer import OrderProducer
from FanOutPattern.core.dispatcher import FanOutDispatcher
 
class FanOutBll(object):
    """
    业务处理
    """
    def demo(self):
        """
 
        :return:
        """
        print("=" * 60)
        print("   珠宝行业企业级 Fan-Out 分布式任务系统 启动成功")
        print("=" * 60)
 
        # 初始化核心组件
        producer = OrderProducer()
        dispatcher = FanOutDispatcher()
 
        # 启动分发器(后台线程)
        dispatcher_thread = threading.Thread(target=dispatcher.start)
        dispatcher_thread.start()
 
        # 启动生产者
        producer.produce()
 
        # 等待结束
        dispatcher_thread.join()
 
        print("\n" + "=" * 60)
        print("   所有订单处理完成,系统安全退出")
        print("=" * 60)


输出:

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐