Files
tushare/start_continuous_learning_system.py
BrunoGao 904b0698ab feat: 新增持续学习系统与Docker部署优化
- 新增持续学习系统完整架构
- 添加AI训练自动化调度器和模型版本管理
- 优化Docker配置支持实时系统
- 新增数据源管理系统集成
- 改进WebSocket和API端口配置
- 添加系统监控和健康检查功能
2025-09-18 22:08:45 +08:00

233 lines
8.4 KiB
Python

#!/usr/bin/env python3
"""
持续学习系统集成启动脚本
Continuous Learning System Integrated Startup Script
"""
import asyncio
import threading
import logging
import sys
import os
import time
import signal
from multiprocessing import Process
# 添加项目路径
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
class ContinuousLearningSystemLauncher:
"""持续学习系统启动器"""
def __init__(self):
self.flask_process = None
self.websocket_process = None
self.running = False
def start_flask_api(self):
"""启动Flask API服务器"""
try:
from api.app import app
logger.info("🚀 启动Flask API服务器 (端口: 5005)")
app.run(host='0.0.0.0', port=5005, debug=False, use_reloader=False)
except Exception as e:
logger.error(f"Flask API启动失败: {e}")
def start_websocket_server(self):
"""启动WebSocket服务器"""
try:
from api.continuous_learning_websocket import start_websocket_server
logger.info("🚀 启动WebSocket服务器 (端口: 8766)")
asyncio.run(start_websocket_server())
except Exception as e:
logger.error(f"WebSocket服务器启动失败: {e}")
def signal_handler(self, signum, frame):
"""信号处理器"""
logger.info("接收到停止信号,正在关闭服务...")
self.stop()
sys.exit(0)
def start(self):
"""启动所有服务"""
logger.info("=" * 60)
logger.info("🎯 启动LJWX股票持续学习系统")
logger.info("=" * 60)
# 注册信号处理器
signal.signal(signal.SIGINT, self.signal_handler)
signal.signal(signal.SIGTERM, self.signal_handler)
self.running = True
try:
# 启动Flask API服务器(独立进程)
self.flask_process = Process(target=self.start_flask_api)
self.flask_process.start()
logger.info("✅ Flask API服务器进程已启动")
# 等待一下让Flask服务器启动
time.sleep(2)
# 启动WebSocket服务器(独立进程)
self.websocket_process = Process(target=self.start_websocket_server)
self.websocket_process.start()
logger.info("✅ WebSocket服务器进程已启动")
logger.info("=" * 60)
logger.info("🎉 持续学习系统启动完成!")
logger.info("📊 管理面板: http://localhost:5005/admin/continuous-learning")
logger.info("🌐 API文档: http://localhost:5005/api/continuous-learning/status")
logger.info("🔌 WebSocket: ws://localhost:8766")
logger.info("=" * 60)
# 监控进程状态
self.monitor_processes()
except Exception as e:
logger.error(f"启动失败: {e}")
self.stop()
def monitor_processes(self):
"""监控进程状态"""
while self.running:
try:
# 检查Flask进程
if self.flask_process and not self.flask_process.is_alive():
logger.error("Flask进程已停止")
break
# 检查WebSocket进程
if self.websocket_process and not self.websocket_process.is_alive():
logger.error("WebSocket进程已停止")
break
# 等待5秒后再次检查
time.sleep(5)
except KeyboardInterrupt:
break
self.stop()
def stop(self):
"""停止所有服务"""
if not self.running:
return
logger.info("🛑 正在停止持续学习系统...")
self.running = False
# 停止Flask进程
if self.flask_process and self.flask_process.is_alive():
logger.info("停止Flask API服务器...")
self.flask_process.terminate()
self.flask_process.join(timeout=5)
if self.flask_process.is_alive():
self.flask_process.kill()
# 停止WebSocket进程
if self.websocket_process and self.websocket_process.is_alive():
logger.info("停止WebSocket服务器...")
self.websocket_process.terminate()
self.websocket_process.join(timeout=5)
if self.websocket_process.is_alive():
self.websocket_process.kill()
logger.info("✅ 持续学习系统已停止")
def test_system_integration():
"""测试系统集成"""
import requests
import json
logger.info("🧪 测试系统集成...")
# 测试Flask API
try:
response = requests.get('http://localhost:5005/api/continuous-learning/health', timeout=5)
if response.status_code == 200:
logger.info("✅ Flask API响应正常")
else:
logger.warning(f"⚠️ Flask API响应异常: {response.status_code}")
except Exception as e:
logger.error(f"❌ Flask API测试失败: {e}")
# 测试WebSocket连接
try:
import websockets
async def test_websocket():
try:
async with websockets.connect('ws://localhost:8766') as websocket:
# 发送测试消息
test_message = json.dumps({'type': 'ping'})
await websocket.send(test_message)
# 接收响应
response = await websocket.recv()
data = json.loads(response)
if data.get('type') == 'pong':
logger.info("✅ WebSocket连接正常")
else:
logger.warning(f"⚠️ WebSocket响应异常: {data}")
except Exception as e:
logger.error(f"❌ WebSocket测试失败: {e}")
asyncio.run(test_websocket())
except ImportError:
logger.warning("⚠️ websockets库未安装,跳过WebSocket测试")
except Exception as e:
logger.error(f"❌ WebSocket测试失败: {e}")
def show_system_info():
"""显示系统信息"""
print("\n" + "=" * 80)
print("🎯 LJWX股票智能分析系统 - 持续学习模块")
print("=" * 80)
print("📋 功能模块:")
print(" • 增量数据管道 - 实时数据收集与处理")
print(" • 模型验证框架 - 自动化模型质量评估")
print(" • 训练调度器 - 智能训练任务调度")
print(" • 版本管理系统 - 模型版本控制与部署")
print(" • A/B测试框架 - 模型性能对比测试")
print(" • 实时监控面板 - WebSocket实时数据展示")
print("")
print("🌐 访问地址:")
print(" • 管理面板: http://localhost:5005/admin/continuous-learning")
print(" • 系统状态: http://localhost:5005/api/continuous-learning/status")
print(" • 健康检查: http://localhost:5005/api/continuous-learning/health")
print(" • WebSocket: ws://localhost:8766")
print("")
print("🔧 使用方法:")
print(" python start_continuous_learning_system.py start # 启动系统")
print(" python start_continuous_learning_system.py test # 测试集成")
print(" python start_continuous_learning_system.py info # 显示信息")
print("=" * 80)
if __name__ == '__main__':
if len(sys.argv) > 1:
command = sys.argv[1].lower()
if command == 'start':
launcher = ContinuousLearningSystemLauncher()
launcher.start()
elif command == 'test':
test_system_integration()
elif command == 'info':
show_system_info()
else:
print("使用方法: python start_continuous_learning_system.py [start|test|info]")
else:
# 默认显示信息并启动
show_system_info()
print("\n启动系统中...")
launcher = ContinuousLearningSystemLauncher()
launcher.start()