diff --git a/agentmain.py b/agentmain.py index 7c4a8a86..30754465 100644 --- a/agentmain.py +++ b/agentmain.py @@ -209,9 +209,13 @@ def run(self): with open(infile, encoding='utf-8') as f: raw = f.read() while True: dq = agent.put_task(raw, source='task') - while 'done' not in (item := dq.get(timeout=300)): - if 'next' in item and random.random() < 0.95: # 概率写一次中间结果 - with open(f'{d}/output{nround}.txt', 'w', encoding='utf-8') as f: f.write(item.get('next', '')) + try: + while 'done' not in (item := dq.get(timeout=300)): + if 'next' in item and random.random() < 0.95: # 概率写一次中间结果 + with open(f'{d}/output{nround}.txt', 'w', encoding='utf-8') as f: f.write(item.get('next', '')) + except queue.Empty: + with open(f'{d}/output{nround}.txt', 'w', encoding='utf-8') as f: f.write('[ERROR] LLM response timeout (300s)\n\n[ROUND END]\n') + break with open(f'{d}/output{nround}.txt', 'w', encoding='utf-8') as f: f.write(item['done'] + '\n\n[ROUND END]\n') consume_file(d, '_stop') # 已经成功停下来了,避免打断下次reply for _ in range(300): # 等reply.txt,10分钟超时