修复子进程日志输出
This commit is contained in:
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -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发送数据"""
|
||||
|
||||
Reference in New Issue
Block a user