#!/usr/bin/env python3 """ 重启API服务器以应用代码更改 """ import subprocess import time import os import signal def restart_api(): print("正在重启API服务器...") try: # 启动新的API服务器 print("启动API服务器...") cmd = ["uv", "run", "./server/api.py"] # 在Windows上后台启动 if os.name == 'nt': process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags=subprocess.CREATE_NEW_CONSOLE) else: process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # 等待一段时间让服务器启动 time.sleep(3) # 检查进程状态 if process.poll() is None: print(f"✓ API服务器已启动 (PID: {process.pid})") print("服务器地址: http://localhost:5000") print("可以通过浏览器或前端访问API") # 测试API是否响应 try: import urllib.request with urllib.request.urlopen('http://localhost:5000/api/models') as response: print(f"✓ API测试成功 (状态码: {response.status})") except Exception as e: print(f"API测试失败: {e}") else: stdout, stderr = process.communicate() print(f"✗ API服务器启动失败") print(f"错误信息: {stderr.decode('utf-8', errors='ignore')}") except Exception as e: print(f"重启失败: {e}") if __name__ == "__main__": restart_api()