92 lines
2.8 KiB
Python
92 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
后台启动Web服务器脚本
|
||
"""
|
||
|
||
import os
|
||
import sys
|
||
import subprocess
|
||
import signal
|
||
import time
|
||
import logging
|
||
|
||
logging.basicConfig(level=logging.INFO)
|
||
logger = logging.getLogger(__name__)
|
||
|
||
def start_web_server():
|
||
"""在后台启动Web服务器"""
|
||
try:
|
||
logger.info("🚀 启动优化版Web服务器...")
|
||
|
||
# 启动Web应用进程
|
||
process = subprocess.Popen([
|
||
sys.executable, 'web_app_vdb_production.py'
|
||
], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||
|
||
logger.info(f"✅ Web服务器已启动,PID: {process.pid}")
|
||
logger.info("🌐 服务地址: http://127.0.0.1:5000")
|
||
|
||
# 等待几秒让服务器完全启动
|
||
time.sleep(5)
|
||
|
||
return process
|
||
|
||
except Exception as e:
|
||
logger.error(f"❌ 启动Web服务器失败: {e}")
|
||
return None
|
||
|
||
def stop_web_server(process):
|
||
"""停止Web服务器"""
|
||
if process:
|
||
try:
|
||
process.terminate()
|
||
process.wait(timeout=5)
|
||
logger.info("✅ Web服务器已停止")
|
||
except subprocess.TimeoutExpired:
|
||
process.kill()
|
||
logger.info("🔥 强制停止Web服务器")
|
||
except Exception as e:
|
||
logger.error(f"❌ 停止Web服务器失败: {e}")
|
||
|
||
if __name__ == "__main__":
|
||
import argparse
|
||
|
||
parser = argparse.ArgumentParser(description='Web服务器管理')
|
||
parser.add_argument('action', choices=['start', 'test'],
|
||
help='操作: start(启动服务器) 或 test(启动并运行测试)')
|
||
|
||
args = parser.parse_args()
|
||
|
||
if args.action == 'start':
|
||
# 只启动服务器
|
||
process = start_web_server()
|
||
if process:
|
||
try:
|
||
logger.info("按 Ctrl+C 停止服务器")
|
||
process.wait()
|
||
except KeyboardInterrupt:
|
||
logger.info("🛑 用户停止服务")
|
||
stop_web_server(process)
|
||
|
||
elif args.action == 'test':
|
||
# 启动服务器并运行测试
|
||
process = start_web_server()
|
||
if process:
|
||
try:
|
||
# 运行测试
|
||
logger.info("🧪 运行优化系统测试...")
|
||
test_result = subprocess.run([
|
||
sys.executable, 'test_optimized_system.py'
|
||
], capture_output=True, text=True)
|
||
|
||
print(test_result.stdout)
|
||
if test_result.stderr:
|
||
print("STDERR:", test_result.stderr)
|
||
|
||
logger.info(f"测试完成,退出码: {test_result.returncode}")
|
||
|
||
finally:
|
||
# 停止服务器
|
||
stop_web_server(process)
|