# Qwen-Agent 统一启动脚本使用说明 本文档介绍了优化后的启动脚本,整合了 FastAPI 应用和队列消费者。 ## 启动脚本选项 ### 1. Python 统一启动脚本 (推荐) **文件**: `start_unified.py` **特点**: - 单一 Python 脚本管理所有服务 - 自动重启功能 - 实时性能监控 - 优雅关闭处理 - 彩色输出和日志 **使用方法**: ```bash # 基本使用 python3 start_unified.py # 自定义配置 python3 start_unified.py \ --host 0.0.0.0 \ --port 8001 \ --api-workers 4 \ --queue-workers 2 \ --profile balanced \ --log-level info # 高性能模式 python3 start_unified.py --profile high_performance --api-workers 8 # 低内存模式 python3 start_unified.py --profile low_memory --api-workers 2 ``` **命令行参数**: ``` --host API绑定主机地址 (默认: 0.0.0.0) --port API绑定端口 (默认: 8001) --api-workers API工作进程数量 (默认: 自动计算) --queue-workers 队列消费者工作线程数 (默认: 2) --worker-type 队列工作类型 (threads/greenlets/gevent, 默认: threads) --profile 性能配置文件 (low_memory/balanced/high_performance, 默认: balanced) --log-level 日志级别 (debug/info/warning/error, 默认: info) --check-interval 进程检查间隔秒数 (默认: 5) --max-restarts 最大重启次数 (默认: 3) ``` ### 2. Bash 统一启动脚本 **文件**: `start_all_optimized.sh` **特点**: - 纯 Bash 实现 - 环境变量配置 - 自动重启 - 进程监控 - 优雅关闭 **使用方法**: ```bash # 基本使用 ./start_all_optimized.sh # 使用环境变量配置 HOST=0.0.0.0 \ PORT=8001 \ API_WORKERS=4 \ QUEUE_WORKERS=2 \ PROFILE=balanced \ ./start_all_optimized.sh # 高性能模式 PROFILE=high_performance API_WORKERS=8 ./start_all_optimized.sh # 查看帮助 ./start_all_optimized.sh --help ``` **环境变量**: ```bash HOST API绑定主机地址 (默认: 0.0.0.0) PORT API绑定端口 (默认: 8001) API_WORKERS API工作进程数 (默认: 4) QUEUE_WORKERS 队列工作线程数 (默认: 2) PROFILE 性能配置文件 (默认: balanced) LOG_LEVEL 日志级别 (默认: info) MAX_RESTARTS 最大重启次数 (默认: 3) CHECK_INTERVAL 健康检查间隔秒数 (默认: 5) ``` ### 3. 单独的队列消费者 (可选) **文件**: `task_queue/optimized_consumer.py` **使用方法**: ```bash # 基本使用 python3 task_queue/optimized_consumer.py # 自定义配置 python3 task_queue/optimized_consumer.py \ --workers 4 \ --worker-type threads \ --profile balanced # 检查队列状态 python3 task_queue/optimized_consumer.py --check-status ``` ## 性能配置文件 ### low_memory (低内存模式) - 适合内存受限环境 (< 4GB) - 较少的缓存和线程 - 适合小型部署 ```yaml 配置: api_workers: 2 queue_workers: 2 agent_cache: 20 shard_count: 8 file_cache: 500 tokenizer_parallelism: false ``` ### balanced (平衡模式) - 默认推荐 - 平衡性能和资源使用 - 适合大多数部署场景 - 推荐用于生产环境 ```yaml 配置: api_workers: 4 queue_workers: 2 agent_cache: 50 shard_count: 16 file_cache: 1000 tokenizer_parallelism: true ``` ### high_performance (高性能模式) - 最大化性能 - 需要充足内存 (> 8GB) - 适合高并发场景 ```yaml 配置: api_workers: 8 queue_workers: 4 agent_cache: 100 shard_count: 32 file_cache: 2000 tokenizer_parallelism: true ``` ## 监控和日志 ### 性能监控 API ```bash # 获取性能统计 curl http://localhost:8001/api/v1/system/performance # 获取系统配置 curl http://localhost:8001/api/v1/system/config # 清理缓存 curl -X POST http://localhost:8001/api/v1/system/clear-cache ``` ### 日志文件 - Python 脚本: 实时输出到控制台 - Bash 脚本: - API日志: `api_server.log` - 队列日志: `queue_consumer.log` ### 队列状态监控 ```bash # 检查队列状态 python3 task_queue/optimized_consumer.py --check-status ``` ## 故障排除 ### 常见问题 1. **端口被占用** ```bash # 检查端口使用 lsof -i :8001 # 或使用其他端口 PORT=8080 ./start_all_optimized.sh ``` 2. **内存不足** ```bash # 使用低内存模式 ./start_all_optimized.sh -p low_memory ``` 3. **依赖缺失** ```bash # 安装依赖 pip install -r requirements_optimization.txt ``` 4. **服务无法启动** ```bash # 检查日志 tail -f api_server.log tail -f queue_consumer.log # 检查进程状态 ps aux | grep uvicorn ps aux | grep consumer ``` ### 性能调优建议 1. **CPU 密集型任务** - 增加 API 工作进程数 - 使用 high_performance 配置 2. **IO 密集型任务** - 增加队列消费者线程数 - 确保磁盘性能足够 3. **内存优化** - 定期清理缓存 - 使用低内存配置 - 监控内存使用率 ## 生产环境部署 ### Docker 部署示例 ```dockerfile FROM python:3.9 WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . # 使用优化的启动脚本 CMD ["python3", "start_unified.py", "--profile", "balanced"] ``` ### Systemd 服务示例 ```ini [Unit] Description=Qwen Agent Service After=network.target [Service] Type=simple User=www-data WorkingDirectory=/opt/qwen-agent ExecStart=/usr/bin/python3 /opt/qwen-agent/start_unified.py --profile balanced Restart=always RestartSec=10 [Install] WantedBy=multi-user.target ``` ### Nginx 反向代理 ```nginx server { listen 80; server_name your-domain.com; location / { proxy_pass http://127.0.0.1:8001; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } ``` ## 迁移指南 ### 从原始 start_all.sh 迁移 1. **停止现有服务** ```bash # 如果原始脚本还在运行 pkill -f "uvicorn fastapi_app" pkill -f "task_queue/consumer.py" ``` 2. **使用新的启动脚本** ```bash # 推荐: Python 脚本 python3 start_unified.py --profile balanced # 或: Bash 脚本 ./start_all_optimized.sh ``` 3. **验证服务** ```bash # 检查 API 服务 curl http://localhost:8001/api/health # 检查队列状态 python3 task_queue/optimized_consumer.py --check-status ``` ## 性能对比 | 项目 | 原始版本 | 优化版本 | 提升幅度 | |------|----------|----------|----------| | 并发处理 | 单线程 | 多线程/多进程 | 3-5x | | 内存效率 | 无缓存 | 智能缓存 | 30-50% | | 连接复用 | 无连接池 | 连接池 | 80% | | 锁竞争 | 全局锁 | 分片锁 | 显著改善 | | 启动时间 | 串行启动 | 并行启动 | 40% | | 故障恢复 | 无自动重启 | 自动重启 | 新增功能 | 通过使用优化的启动脚本,您可以获得更好的性能、更高的可靠性和更简单的运维体验。