import asyncio import sys import signal from config.settings import load_config, check_config_file from core.websocket_server import WebSocketServer from core.utils.util import check_ffmpeg_installed from core.utils.mysql_util import init_mysql_pool, close_mysql_pool TAG = __name__ async def wait_for_exit(): """Windows 和 Linux 兼容的退出监听""" loop = asyncio.get_running_loop() stop_event = asyncio.Event() if sys.platform == "win32": # Windows: 用 sys.stdin.read() 监听 Ctrl + C await loop.run_in_executor(None, sys.stdin.read) else: # Linux/macOS: 用 signal 监听 Ctrl + C def stop(): stop_event.set() loop.add_signal_handler(signal.SIGINT, stop) loop.add_signal_handler(signal.SIGTERM, stop) # 支持 kill 进程 await stop_event.wait() async def main(): # 检查配置文件 check_config_file() # 检查 ffmpeg 是否安装 check_ffmpeg_installed() # 加载配置 config = load_config() # 初始化 MySQL 连接池 await init_mysql_pool() # 启动 WebSocket 服务器 ws_server = WebSocketServer(config) ws_task = asyncio.create_task(ws_server.start()) try: await wait_for_exit() # 监听退出信号 except asyncio.CancelledError: print("任务被取消,清理资源中...") finally: ws_task.cancel() try: await ws_task except asyncio.CancelledError: pass # 关闭 MySQL 连接池 await close_mysql_pool() print("服务器已关闭,程序退出。") if __name__ == "__main__": try: asyncio.run(main()) except KeyboardInterrupt: print("手动中断,程序终止。")