mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-28 10:03:54 +08:00
Merge pull request #1104 from CaixyPromise/feature/wait-exit-fix
fix(app): 修复app.py内的wait_for_exit(),以此解决Windows环境下手动退出时,进程阻塞卡死的问题。
This commit is contained in:
+16
-12
@@ -12,22 +12,26 @@ TAG = __name__
|
|||||||
logger = setup_logging()
|
logger = setup_logging()
|
||||||
|
|
||||||
|
|
||||||
async def wait_for_exit():
|
async def wait_for_exit() -> None:
|
||||||
"""Windows 和 Linux 兼容的退出监听"""
|
"""
|
||||||
|
阻塞直到收到 Ctrl‑C / SIGTERM。
|
||||||
|
- Unix: 使用 add_signal_handler
|
||||||
|
- Windows: 依赖 KeyboardInterrupt
|
||||||
|
"""
|
||||||
loop = asyncio.get_running_loop()
|
loop = asyncio.get_running_loop()
|
||||||
stop_event = asyncio.Event()
|
stop_event = asyncio.Event()
|
||||||
|
|
||||||
if sys.platform == "win32":
|
if sys.platform != "win32": # Unix / macOS
|
||||||
# Windows: 用 sys.stdin.read() 监听 Ctrl + C
|
for sig in (signal.SIGINT, signal.SIGTERM):
|
||||||
await loop.run_in_executor(None, sys.stdin.read)
|
loop.add_signal_handler(sig, stop_event.set)
|
||||||
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()
|
await stop_event.wait()
|
||||||
|
else:
|
||||||
|
# Windows:await一个永远pending的fut,
|
||||||
|
# 让 KeyboardInterrupt 冒泡到 asyncio.run,以此消除遗留普通线程导致进程退出阻塞的问题
|
||||||
|
try:
|
||||||
|
await asyncio.Future()
|
||||||
|
except KeyboardInterrupt: # Ctrl‑C
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
|
|||||||
Reference in New Issue
Block a user