mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 07:03:53 +08:00
fix(app): 重写app.py内的wait_for_exit(),以此解决Windows环境下手动退出时,进程阻塞卡死的问题。
影响 ---- - Ctrl‑C/kill退出时不再卡住,资源完全释放, - Windows 与 Unix 行为一致,改动不影响正常业务逻辑。
This commit is contained in:
+16
-12
@@ -12,22 +12,26 @@ TAG = __name__
|
||||
logger = setup_logging()
|
||||
|
||||
|
||||
async def wait_for_exit():
|
||||
"""Windows 和 Linux 兼容的退出监听"""
|
||||
async def wait_for_exit() -> None:
|
||||
"""
|
||||
阻塞直到收到 Ctrl‑C / SIGTERM。
|
||||
- Unix: 使用 add_signal_handler
|
||||
- Windows: 依赖 KeyboardInterrupt
|
||||
"""
|
||||
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 进程
|
||||
if sys.platform != "win32": # Unix / macOS
|
||||
for sig in (signal.SIGINT, signal.SIGTERM):
|
||||
loop.add_signal_handler(sig, stop_event.set)
|
||||
await stop_event.wait()
|
||||
else:
|
||||
# Windows:await一个永远pending的fut,
|
||||
# 让 KeyboardInterrupt 冒泡到 asyncio.run,以此消除遗留普通线程导致进程退出阻塞的问题
|
||||
try:
|
||||
await asyncio.Future()
|
||||
except KeyboardInterrupt: # Ctrl‑C
|
||||
pass
|
||||
|
||||
|
||||
async def main():
|
||||
|
||||
Reference in New Issue
Block a user