修复子进程日志输出
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(
|
self.process = subprocess.Popen(
|
||||||
['python', 'Flask/FlaskService.py'], # 启动一个子进程 FlaskService.py
|
['python', 'Flask/FlaskService.py'], # 启动一个子进程 FlaskService.py
|
||||||
stdin=subprocess.PIPE, # 标准输入流,用于向子进程发送数据
|
stdin=subprocess.PIPE, # 标准输入流,用于向子进程发送数据
|
||||||
stdout=subprocess.PIPE, # 标准输出流,用于接收子进程的输出
|
stdout=subprocess.PIPE, # 标准输出流,用于接收子进程的输出
|
||||||
stderr=subprocess.PIPE, # 标准错误流,用于接收子进程的错误信息
|
stderr=subprocess.PIPE, # 标准错误流,用于接收子进程的错误信息
|
||||||
text=True, # 以文本模式打开流,否则以二进制模式打开
|
text=True, # 以文本模式打开流,否则以二进制模式打开
|
||||||
@@ -52,20 +52,16 @@ class FlaskSubprocessManager:
|
|||||||
print(f"Flask子进程启动 (PID: {self.process.pid}, 通信端口: {self.comm_port})")
|
print(f"Flask子进程启动 (PID: {self.process.pid}, 通信端口: {self.comm_port})")
|
||||||
|
|
||||||
# 将日志通过主进程输出
|
# 将日志通过主进程输出
|
||||||
def print_output():
|
def print_output(stream, stream_name):
|
||||||
while True:
|
while True:
|
||||||
output = self.process.stdout.readline()
|
line = stream.readline()
|
||||||
if not output:
|
if not line:
|
||||||
break
|
break
|
||||||
print(output.strip())
|
print(f"{stream_name}: {line.strip()}")
|
||||||
|
|
||||||
while True:
|
# 启动两个线程分别处理 stdout 和 stderr
|
||||||
error = self.process.stderr.readline()
|
threading.Thread(target=print_output, args=(self.process.stdout, "STDOUT"), daemon=True).start()
|
||||||
if not error:
|
threading.Thread(target=print_output, args=(self.process.stderr, "STDERR"), daemon=True).start()
|
||||||
break
|
|
||||||
print(f"Error: {error.strip()}")
|
|
||||||
|
|
||||||
threading.Thread(target=print_output, daemon=True).start()
|
|
||||||
|
|
||||||
def send(self, data: Union[str, Dict, List]) -> bool:
|
def send(self, data: Union[str, Dict, List]) -> bool:
|
||||||
"""通过Socket发送数据"""
|
"""通过Socket发送数据"""
|
||||||
|
|||||||
Reference in New Issue
Block a user