From cf8c191ae9608c5882e3a0886fa9f57f542705e5 Mon Sep 17 00:00:00 2001 From: Liang Jiaqing Date: Sat, 14 Feb 2026 10:13:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E8=B0=83=E5=BA=A6?= =?UTF-8?q?=E5=99=A8=E5=8D=95=E4=BE=8B=E6=8E=A7=E5=88=B6=E5=92=8Claunch?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E9=9B=86=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - agentmain.py: 添加单例锁机制,防止重复启动 - launch.pyw: 集成调度器启动,支持--no-scheduler参数 --- agentmain.py | 25 +++++++++++++++++++++++++ launch.pyw | 7 +++++++ 2 files changed, 32 insertions(+) diff --git a/agentmain.py b/agentmain.py index 45f1727..d14d6c7 100644 --- a/agentmain.py +++ b/agentmain.py @@ -110,8 +110,33 @@ 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 3e036b9..317edf5 100644 --- a/launch.pyw +++ b/launch.pyw @@ -79,6 +79,7 @@ if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('port', nargs='?', default='8501') parser.add_argument('--no-tg', action='store_true', help='不启动 Telegram Bot') + parser.add_argument('--no-scheduler', action='store_true', help='不启动计划任务调度器') args = parser.parse_args() port = args.port t = threading.Thread(target=start_streamlit, args=(port,), daemon=True) @@ -89,6 +90,12 @@ if __name__ == '__main__': atexit.register(tgproc.kill) print('[Launch] Telegram Bot started') 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') + else: print('[Launch] Task Scheduler disabled (--no-scheduler)') monitor_thread = threading.Thread(target=idle_monitor, daemon=True) monitor_thread.start()