feat: AI并行处理 + 修复邮件丢失

- _ai_processor 改用 ThreadPoolExecutor(5 workers) 并行调用AI
- _tg_worker 每轮排空所有待发TG消息(原来每轮只取1条)
- fetch_unseen_emails 单封FETCH失败不再丢弃整个批次,跳过继续
This commit is contained in:
2026-07-06 20:12:24 +08:00
parent 0c1ccdfc79
commit d70b817fd4
2 changed files with 57 additions and 39 deletions

24
main.py
View File

@@ -5,6 +5,7 @@ import signal
import sys import sys
import threading import threading
import time import time
from concurrent.futures import ThreadPoolExecutor
from src.config import load_config from src.config import load_config
from src.summarizer import poll_accounts, ai_process, tg_send_and_mark from src.summarizer import poll_accounts, ai_process, tg_send_and_mark
from src.tg_bot import poll_telegram from src.tg_bot import poll_telegram
@@ -64,14 +65,24 @@ def _email_poller(cfg):
def _ai_processor(cfg): def _ai_processor(cfg):
logger.info("AI 处理线程已启动") logger.info("AI 处理线程已启动 (并行 workers=5)")
max_workers = 5
with ThreadPoolExecutor(max_workers=max_workers) as executor:
pending_futures = {}
while _running: while _running:
while len(pending_futures) < max_workers:
try: try:
acct_idx, mail = _email_queue.get(timeout=1) acct_idx, mail = _email_queue.get_nowait()
except queue_module.Empty: except queue_module.Empty:
continue break
future = executor.submit(ai_process, cfg, acct_idx, mail)
pending_futures[future] = (acct_idx, mail)
completed = [f for f in pending_futures if f.done()]
for future in completed:
acct_idx, mail = pending_futures.pop(future)
try: try:
info = ai_process(cfg, acct_idx, mail) info = future.result()
if info: if info:
_tg_queue.put(info) _tg_queue.put(info)
except Exception as e: except Exception as e:
@@ -80,6 +91,8 @@ def _ai_processor(cfg):
with _pending_lock: with _pending_lock:
_pending_uids.discard(mail.uid) _pending_uids.discard(mail.uid)
time.sleep(0.1 if pending_futures else 0.5)
def _tg_worker(cfg): def _tg_worker(cfg):
logger.info("TG 线程已启动") logger.info("TG 线程已启动")
@@ -90,11 +103,12 @@ def _tg_worker(cfg):
except Exception as e: except Exception as e:
logger.error(f"TG 轮询错误: {e}", exc_info=True) logger.error(f"TG 轮询错误: {e}", exc_info=True)
while _running:
try: try:
info = _tg_queue.get_nowait() info = _tg_queue.get_nowait()
tg_send_and_mark(cfg, info) tg_send_and_mark(cfg, info)
except queue_module.Empty: except queue_module.Empty:
pass break
except Exception as e: except Exception as e:
logger.error(f"TG 发送失败: {e}", exc_info=True) logger.error(f"TG 发送失败: {e}", exc_info=True)

View File

@@ -115,6 +115,7 @@ def fetch_unseen_emails(account: EmailAccount) -> list[Email]:
emails = [] emails = []
for uid in uids: for uid in uids:
try:
_, msg_data = conn.uid("FETCH", uid, "RFC822") _, msg_data = conn.uid("FETCH", uid, "RFC822")
if msg_data[0] is None: if msg_data[0] is None:
logger.warning("UID %s FETCH 返回空,跳过", uid) logger.warning("UID %s FETCH 返回空,跳过", uid)
@@ -133,6 +134,9 @@ def fetch_unseen_emails(account: EmailAccount) -> list[Email]:
logger.info(" 邮件: [%s] from=%s to=%s reply-to=%s (%d 字符)", subject, sender, recipient, reply_to, body_len) logger.info(" 邮件: [%s] from=%s to=%s reply-to=%s (%d 字符)", subject, sender, recipient, reply_to, body_len)
emails.append(Email(uid=uid, subject=subject, sender=sender, recipient=recipient, emails.append(Email(uid=uid, subject=subject, sender=sender, recipient=recipient,
body=body, date=date_str, reply_to=reply_to, account_email=account.username)) body=body, date=date_str, reply_to=reply_to, account_email=account.username))
except Exception as e:
logger.error("UID %s FETCH 失败,跳过: %s", uid, e)
continue
conn.logout() conn.logout()
logger.info("共获取 %d 封新邮件", len(emails)) logger.info("共获取 %d 封新邮件", len(emails))