# Copyright 2026 zhaoxi826 # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import ray from functools import lru_cache class ActorList: """ActorList 核心组件类。 这是一个领域数据模型或功能封装类,承载了 ActorList 相关的内聚属性定义与状态维护。它的存在隔离了局部的业务复杂性,并对外提供了类型安全的访问接口。""" def __init__(self): super().__setattr__("dict", {}) def __setattr__(self, key, value): """对现有的 setattr 进行状态更新或属性覆盖。 基于增量变更原则,合并最新的配置或数据,并触发相关依赖组件的缓存刷新或事件通知。 Args: key: 参与 setattr 逻辑运算或数据构建的上下文依赖对象。 value: 参与 setattr 逻辑运算或数据构建的上下文依赖对象。""" self.dict[key] = value def __getattr__(self, key): """检索并获取特定的 getattr 数据集合或实例对象。 根据提供的查询条件或上下文凭证,从数据库、缓存或第三方服务中读取对应的资源状态。 Args: key: 参与 getattr 逻辑运算或数据构建的上下文依赖对象。 Returns: : 经由当前业务模型加工处理后所输出的具体数据实例或领域模型对象。""" if key in self.dict: return self.dict[key] raise AttributeError(f"ActorList 对象没有属性 '{key}'") def __delattr__(self, key): """执行与 delattr 相关的核心业务流转操作。 该方法封装了具体的算法策略或状态控制逻辑,确保操作能够在事务上下文中被原子且一致地执行。 Args: key: 参与 delattr 逻辑运算或数据构建的上下文依赖对象。""" if key in self.dict: del self.dict[key] else: raise AttributeError(f"ActorList对象没有属性 '{key}'") @lru_cache(maxsize=128) def _get_cached_actor_handle(actor_name: str): """缓存接口""" return ray.get_actor(actor_name, namespace="pretor") def clear_actor_cache(): """清理接口""" _get_cached_actor_handle.cache_clear() def ray_actor_hook(*actor_names: str): """执行与 ray actor hook 相关的核心业务流转操作。 该方法封装了具体的算法策略或状态控制逻辑,确保操作能够在事务上下文中被原子且一致地执行。 Returns: : 经由当前业务模型加工处理后所输出的具体数据实例或领域模型对象。""" actor_list = ActorList() for actor_name in actor_names: handle = _get_cached_actor_handle(actor_name) setattr(actor_list, actor_name, handle) return actor_list