129 lines
3.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from abc import ABC, abstractmethod
from queue import Queue, Empty
from typing import List, Callable, Any, Optional
import logging
import threading
import time
# 配置日志
logger = logging.getLogger(__name__)
class PipelineBase(ABC):
"""
管道基类
定义了管道的基本接口和通用功能
"""
def __init__(self, input_queue: Optional[Queue] = None):
"""
初始化管道
参数:
input_queue: 输入队列,用于接收数据
"""
self._input_queue = input_queue
self._callbacks: List[Callable] = []
self._is_running = False
self._stop_event = False
self._thread: Optional[threading.Thread] = None
self._stop_timeout = 5 # 默认停止超时时间(秒)
self._queue_timeout = 1 # 队列获取超时时间(秒)
def set_input_queue(self, queue: Queue) -> None:
"""
设置输入队列
参数:
queue: 输入队列
"""
self._input_queue = queue
def add_callback(self, callback: Callable) -> None:
"""
添加回调函数
参数:
callback: 回调函数,接收处理结果
"""
self._callbacks.append(callback)
def _notify_callbacks(self, result: Any) -> None:
"""
通知所有回调函数
参数:
result: 处理结果
"""
for callback in self._callbacks:
try:
callback(result)
except Exception as e:
logger.error(f"回调函数执行出错: {str(e)}")
@abstractmethod
def process(self, data: Any) -> Any:
"""
处理数据
参数:
data: 输入数据
返回:
处理结果
"""
pass
@abstractmethod
def run(self) -> None:
"""
运行管道
从输入队列获取数据并处理
"""
pass
def stop(self, timeout: Optional[float] = None) -> bool:
"""
停止管道
参数:
timeout: 停止超时时间None表示使用默认超时时间
返回:
bool: 是否成功停止
"""
if not self._is_running:
return True
logger.info("正在停止管道...")
self._stop_event = True
self._is_running = False
# 等待线程结束
if self._thread and self._thread.is_alive():
timeout = timeout if timeout is not None else self._stop_timeout
self._thread.join(timeout=timeout)
# 检查是否成功停止
if self._thread.is_alive():
logger.warning(f"管道停止超时({timeout}秒),强制终止")
return False
else:
logger.info("管道已成功停止")
return True
return True
def force_stop(self) -> None:
"""
强制停止管道
注意:这可能会导致资源未正确释放
"""
logger.warning("强制停止管道")
self._stop_event = True
self._is_running = False
# 注意Python的线程无法被强制终止这里只是设置标志
# 实际终止需要依赖操作系统的进程管理
class PipelineFactory:
"""
管道工厂类
用于创建管道实例
"""
@staticmethod
def create_pipeline(pipeline_name: str) -> Pipeline:
"""
创建管道实例
"""
pass