File tree Expand file tree Collapse file tree 1 file changed +18
-23
lines changed
Expand file tree Collapse file tree 1 file changed +18
-23
lines changed Original file line number Diff line number Diff line change 22import argparse
33import json
44import logging
5- import os
6- import signal
75
86import sentry_sdk
97from sentry_sdk .integrations .asyncio import AsyncioIntegration
@@ -162,37 +160,34 @@ def main():
162160 except Exception as e :
163161 print (f"Execption { e } " )
164162
165- # 全局变量用于信号处理
166- xiaomusic_instance = None
167-
168163 def run_server (port ):
169- nonlocal xiaomusic_instance
170164 xiaomusic_instance = XiaoMusic (config )
171165 HttpInit (xiaomusic_instance )
166+
167+ # 注册退出处理函数
168+ import atexit
169+
170+ def cleanup ():
171+ print ("程序退出,清理资源..." )
172+ if (
173+ xiaomusic_instance
174+ and hasattr (xiaomusic_instance , "js_plugin_manager" )
175+ and xiaomusic_instance .js_plugin_manager
176+ ):
177+ try :
178+ xiaomusic_instance .js_plugin_manager .shutdown ()
179+ except Exception as e :
180+ print (f"关闭 JS 插件管理器时出错: { e } " )
181+
182+ atexit .register (cleanup )
183+
172184 uvicorn .run (
173185 HttpApp ,
174186 host = ["0.0.0.0" , "::" ],
175187 port = port ,
176188 log_config = LOGGING_CONFIG ,
177189 )
178190
179- def signal_handler (sig , frame ):
180- print ("主进程收到退出信号,准备退出..." )
181- # 关闭 JS 插件管理器
182- if (
183- xiaomusic_instance
184- and hasattr (xiaomusic_instance , "js_plugin_manager" )
185- and xiaomusic_instance .js_plugin_manager
186- ):
187- try :
188- xiaomusic_instance .js_plugin_manager .shutdown ()
189- except Exception as e :
190- print (f"关闭 JS 插件管理器时出错: { e } " )
191- os ._exit (0 ) # 退出主进程
192-
193- # 捕获主进程的退出信号
194- signal .signal (signal .SIGINT , signal_handler )
195- signal .signal (signal .SIGTERM , signal_handler )
196191 port = int (config .port )
197192 run_server (port )
198193
You can’t perform that action at this time.
0 commit comments