🐛 Ctrl+C 无法终止Channel接收线程的问题
This commit is contained in:
@ -1,6 +1,8 @@
|
||||
import asyncio
|
||||
import atexit
|
||||
import os
|
||||
import platform
|
||||
import signal
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
@ -38,11 +40,16 @@ class LiteyukiBot:
|
||||
|
||||
self.loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(self.loop)
|
||||
|
||||
self.stop_event = threading.Event()
|
||||
self.call_restart_count = 0
|
||||
|
||||
load_plugins("liteyuki/plugins") # 加载轻雪插件
|
||||
|
||||
signal.signal(signal.SIGINT, self._handle_exit)
|
||||
signal.signal(signal.SIGTERM, self._handle_exit)
|
||||
atexit.register(self.process_manager.terminate_all) # 注册退出时的函数
|
||||
|
||||
def run(self):
|
||||
"""
|
||||
启动逻辑
|
||||
@ -60,12 +67,24 @@ class LiteyukiBot:
|
||||
"""
|
||||
try:
|
||||
while not self.stop_event.is_set():
|
||||
time.sleep(1)
|
||||
time.sleep(0.5)
|
||||
except KeyboardInterrupt:
|
||||
logger.info("Liteyuki is stopping...")
|
||||
self.stop()
|
||||
finally:
|
||||
self.lifespan.after_shutdown()
|
||||
|
||||
def _handle_exit(self, signum, frame):
|
||||
"""
|
||||
信号处理
|
||||
Args:
|
||||
signum:
|
||||
frame:
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
logger.info("Received signal, stopping all processes.")
|
||||
self.stop()
|
||||
sys.exit(0)
|
||||
|
||||
def restart(self, delay: int = 0):
|
||||
"""
|
||||
@ -116,16 +135,12 @@ class LiteyukiBot:
|
||||
Returns:
|
||||
|
||||
"""
|
||||
self.init_config()
|
||||
self.init_logger()
|
||||
|
||||
def init_logger(self):
|
||||
# 修改nonebot的日志配置
|
||||
init_log(config=self.config)
|
||||
|
||||
def init_config(self):
|
||||
pass
|
||||
|
||||
def stop(self):
|
||||
"""
|
||||
停止轻雪
|
||||
|
Reference in New Issue
Block a user