rogerxavier
commited on
Commit
•
96888b9
1
Parent(s):
7497094
Update api.py
Browse files
api.py
CHANGED
@@ -59,8 +59,20 @@ async def delete_files(directory: str):
|
|
59 |
def file_executer(file_name:str)->"执行返回":
|
60 |
try:
|
61 |
print("开始执行py任务",file_name)
|
62 |
-
# result = subprocess.check_output(["python", f"{file_name}.py"]).decode("utf-8")
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
print("执行完成py任务:",file_name,"结果如下")
|
65 |
print(result)
|
66 |
return PlainTextResponse(result)
|
|
|
59 |
def file_executer(file_name:str)->"执行返回":
|
60 |
try:
|
61 |
print("开始执行py任务",file_name)
|
62 |
+
# result = subprocess.check_output(["python", f"{file_name}.py"], stderr=subprocess.STDOUT).decode("utf-8")#执行完成后显示运行py的print
|
63 |
+
##########test 边执行py边显示print+++++++++++++++++++++++++++++++++++++
|
64 |
+
# 开始执行Python 脚本
|
65 |
+
output = []
|
66 |
+
process = subprocess.Popen(["python", f"{file_name}.py"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
67 |
+
# 逐行读取脚本的输出并显示
|
68 |
+
for line in iter(process.stdout.readline, b''):
|
69 |
+
line_text = line.decode('utf-8').strip()
|
70 |
+
print(line_text)
|
71 |
+
output.append(line_text)
|
72 |
+
process.wait()
|
73 |
+
result = '\n'.join(output)
|
74 |
+
##########test 边执行py边显示print+++++++++++++++++++++++++++++++++++++
|
75 |
+
|
76 |
print("执行完成py任务:",file_name,"结果如下")
|
77 |
print(result)
|
78 |
return PlainTextResponse(result)
|