From 1269cf0573fd13e10c2503fb872ca38b1f2db078 Mon Sep 17 00:00:00 2001 From: zw <12345678> Date: Fri, 1 Aug 2025 14:52:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AD=90=E8=BF=9B=E7=A8=8B?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/vcs.xml | 6 ++++++ Module/FlaskSubprocessManager.py | 20 ++++++++------------ 2 files changed, 14 insertions(+), 12 deletions(-) create mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Module/FlaskSubprocessManager.py b/Module/FlaskSubprocessManager.py index 32d6db7..054c3e7 100644 --- a/Module/FlaskSubprocessManager.py +++ b/Module/FlaskSubprocessManager.py @@ -41,7 +41,7 @@ class FlaskSubprocessManager: self.process = subprocess.Popen( ['python', 'Flask/FlaskService.py'], # 启动一个子进程 FlaskService.py - stdin=subprocess.PIPE, # 标准输入流,用于向子进程发送数据 + stdin=subprocess.PIPE, # 标准输入流,用于向子进程发送数据 stdout=subprocess.PIPE, # 标准输出流,用于接收子进程的输出 stderr=subprocess.PIPE, # 标准错误流,用于接收子进程的错误信息 text=True, # 以文本模式打开流,否则以二进制模式打开 @@ -52,20 +52,16 @@ class FlaskSubprocessManager: print(f"Flask子进程启动 (PID: {self.process.pid}, 通信端口: {self.comm_port})") # 将日志通过主进程输出 - def print_output(): + def print_output(stream, stream_name): while True: - output = self.process.stdout.readline() - if not output: + line = stream.readline() + if not line: break - print(output.strip()) + print(f"{stream_name}: {line.strip()}") - while True: - error = self.process.stderr.readline() - if not error: - break - print(f"Error: {error.strip()}") - - threading.Thread(target=print_output, daemon=True).start() + # 启动两个线程分别处理 stdout 和 stderr + threading.Thread(target=print_output, args=(self.process.stdout, "STDOUT"), daemon=True).start() + threading.Thread(target=print_output, args=(self.process.stderr, "STDERR"), daemon=True).start() def send(self, data: Union[str, Dict, List]) -> bool: """通过Socket发送数据"""