From ab48b35ca9927165c51434ca5e2fbff0f6064350 Mon Sep 17 00:00:00 2001 From: Liang Jiaqing Date: Sat, 14 Feb 2026 10:17:26 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8=E5=81=87?= =?UTF-8?q?=E7=AB=AF=E5=8F=A3=E5=AE=9E=E7=8E=B0=E8=B0=83=E5=BA=A6=E5=99=A8?= =?UTF-8?q?=E5=8D=95=E4=BE=8B=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 恢复 agentmain.py 到原始状态 - launch.pyw 使用端口 65432 检测单例 - 避免重复启动调度器实例 --- agentmain.py | 25 ------------------------- launch.pyw | 16 +++++++++++++--- 2 files changed, 13 insertions(+), 28 deletions(-) diff --git a/agentmain.py b/agentmain.py index d14d6c7..45f1727 100644 --- a/agentmain.py +++ b/agentmain.py @@ -110,33 +110,8 @@ class GeneraticAgent: if self.handler is not None: self.handler.code_stop_signal.append(1) -def acquire_singleton_lock(): - """确保只有一个调度器实例运行""" - lock_file = './temp/scheduler.lock' - os.makedirs('./temp', exist_ok=True) - - if os.name == 'nt': # Windows - import msvcrt - try: - lock_fd = open(lock_file, 'w') - msvcrt.locking(lock_fd.fileno(), msvcrt.LK_NBLCK, 1) - return lock_fd - except: - print('[Scheduler] Another instance is already running') - sys.exit(0) - else: # Unix/Linux - import fcntl - try: - lock_fd = open(lock_file, 'w') - fcntl.flock(lock_fd.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) - return lock_fd - except: - print('[Scheduler] Another instance is already running') - sys.exit(0) - if __name__ == '__main__': from datetime import datetime - lock_fd = acquire_singleton_lock() # 获取单例锁 agent = GeneraticAgent() threading.Thread(target=agent.run, daemon=True).start() def drain(dq, tag): diff --git a/launch.pyw b/launch.pyw index 317edf5..7e4f212 100644 --- a/launch.pyw +++ b/launch.pyw @@ -92,9 +92,19 @@ if __name__ == '__main__': else: print('[Launch] Telegram Bot disabled (--no-tg)') if not args.no_scheduler: - scheduler_proc = subprocess.Popen([sys.executable, "agentmain.py"], creationflags=subprocess.CREATE_NO_WINDOW if os.name=='nt' else 0) - atexit.register(scheduler_proc.kill) - print('[Launch] Task Scheduler started') + # 使用假端口检测单例 + import socket + scheduler_port = 65432 + try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.bind(('127.0.0.1', scheduler_port)) + sock.listen(1) + # 绑定成功,启动调度器 + scheduler_proc = subprocess.Popen([sys.executable, "agentmain.py"], creationflags=subprocess.CREATE_NO_WINDOW if os.name=='nt' else 0) + atexit.register(lambda: (scheduler_proc.kill(), sock.close())) + print('[Launch] Task Scheduler started') + except OSError: + print('[Launch] Task Scheduler already running (port occupied)') else: print('[Launch] Task Scheduler disabled (--no-scheduler)') monitor_thread = threading.Thread(target=idle_monitor, daemon=True)