fix: use threading.Event for immediate shutdown instead of sleep loops
This commit is contained in:
12
main.py
12
main.py
@@ -28,6 +28,7 @@ _log_listener.start()
|
|||||||
logger = logging.getLogger("main")
|
logger = logging.getLogger("main")
|
||||||
|
|
||||||
_running = True
|
_running = True
|
||||||
|
_shutdown_event = threading.Event()
|
||||||
_pending_lock = threading.Lock()
|
_pending_lock = threading.Lock()
|
||||||
_pending_uids: set[bytes] = set()
|
_pending_uids: set[bytes] = set()
|
||||||
_processing_uids: set[bytes] = set() # 正在处理(含等待TG发送)的邮件UID
|
_processing_uids: set[bytes] = set() # 正在处理(含等待TG发送)的邮件UID
|
||||||
@@ -42,6 +43,7 @@ def _signal_handler(signum, frame):
|
|||||||
global _running
|
global _running
|
||||||
logger.info("收到退出信号,正在停止...")
|
logger.info("收到退出信号,正在停止...")
|
||||||
_running = False
|
_running = False
|
||||||
|
_shutdown_event.set()
|
||||||
|
|
||||||
|
|
||||||
def _email_poller(cfg):
|
def _email_poller(cfg):
|
||||||
@@ -65,10 +67,7 @@ def _email_poller(cfg):
|
|||||||
logger.error(f"邮件轮询线程异常: {e}", exc_info=True)
|
logger.error(f"邮件轮询线程异常: {e}", exc_info=True)
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
if _running:
|
if _running:
|
||||||
for _ in range(cfg.polling.interval_seconds):
|
_shutdown_event.wait(cfg.polling.interval_seconds)
|
||||||
if not _running:
|
|
||||||
return
|
|
||||||
time.sleep(1)
|
|
||||||
|
|
||||||
|
|
||||||
def _ai_processor(cfg):
|
def _ai_processor(cfg):
|
||||||
@@ -129,7 +128,7 @@ def _tg_worker(cfg):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
if _running:
|
if _running:
|
||||||
time.sleep(1)
|
_shutdown_event.wait(1)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -158,9 +157,10 @@ def main():
|
|||||||
if tick % 30 == 0:
|
if tick % 30 == 0:
|
||||||
logger.info("队列状态: email_queue=%d tg_queue=%d pending_uids=%d",
|
logger.info("队列状态: email_queue=%d tg_queue=%d pending_uids=%d",
|
||||||
_email_queue.qsize(), _tg_queue.qsize(), len(_pending_uids))
|
_email_queue.qsize(), _tg_queue.qsize(), len(_pending_uids))
|
||||||
time.sleep(1)
|
_shutdown_event.wait(1)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
_running = False
|
_running = False
|
||||||
|
_shutdown_event.set()
|
||||||
|
|
||||||
for t in threads:
|
for t in threads:
|
||||||
t.join(timeout=5)
|
t.join(timeout=5)
|
||||||
|
|||||||
Reference in New Issue
Block a user