From 14e3a915505c49508bbe5baa0ba957bdb2214f9f Mon Sep 17 00:00:00 2001 From: liujiangning30 <147385819+liujiangning30@users.noreply.github.com> Date: Thu, 21 Nov 2024 20:57:00 +0800 Subject: [PATCH] Fix: http agent server (#276) * Fix the agent service being suspended * remove unnecessary codeline * update comments --- lagent/distributed/http_serve/api_server.py | 24 ++++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/lagent/distributed/http_serve/api_server.py b/lagent/distributed/http_serve/api_server.py index 0cb6907a..e78dcacc 100644 --- a/lagent/distributed/http_serve/api_server.py +++ b/lagent/distributed/http_serve/api_server.py @@ -3,6 +3,7 @@ import subprocess import sys import time +import threading import aiohttp import requests @@ -77,14 +78,21 @@ def start_server(self): stderr=subprocess.STDOUT, text=True) - while True: - output = self.process.stdout.readline() - if not output: # 如果读到 EOF,跳出循环 - break - sys.stdout.write(output) # 打印到标准输出 - sys.stdout.flush() - if 'Uvicorn running on' in output: # 根据实际输出调整 - break + self.service_started = False + + def log_output(stream): + if stream is not None: + for line in iter(stream.readline, ''): + print(line, end='') + if 'Uvicorn running on' in line: + self.service_started = True + + # Start log output thread + threading.Thread(target=log_output, args=(self.process.stdout,), daemon=True).start() + threading.Thread(target=log_output, args=(self.process.stderr,), daemon=True).start() + + # Waiting for the service to start + while not self.service_started: time.sleep(0.1) def shutdown(self):