rogerxavier commited on
Commit
726fca1
1 Parent(s): b80bf3b

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +36 -62
api.py CHANGED
@@ -66,30 +66,30 @@ async def delete_files(directory: str):
66
 
67
 
68
  ########异步处理py文件执行接口
69
- # def file_executer(file_name:str)->"执行返回":
70
- # try:
71
- # print("开始执行py任务",file_name)
72
- # # result = subprocess.check_output(["python", f"{file_name}.py"], stderr=subprocess.STDOUT).decode("utf-8")#执行完成后显示运行py的print
73
- # ##########test 边执行py边显示print+++++++++++++++++++++++++++++++++++++
74
- # # 开始执行Python 脚本
75
- # output = []
76
- # process = subprocess.Popen(["python", f"{file_name}.py"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
77
- # # 逐行读取脚本的输出并显示
78
- # for line in iter(process.stdout.readline, b''):
79
- # line_text = line.decode('utf-8').strip()
80
- # print(line_text)
81
- # output.append(line_text)
82
- # process.wait()
83
- # result = '\n'.join(output)
84
- # ##########test 边执行py边显示print+++++++++++++++++++++++++++++++++++++
85
 
86
- # print("执行完成py任务:",file_name,"结果如下")
87
- # print(result)
88
- # return PlainTextResponse(result)
89
- # except subprocess.CalledProcessError as e:
90
- # print("执行py任务失败",file_name,"原因如下")
91
- # print(f"Error executing {file_name}.py: {e}")
92
- # return PlainTextResponse(f"Error executing {file_name}.py: {e}")
93
 
94
  @app.get("/execute_py_file/{file_name}")
95
  async def execute_py_file(file_name: str,background_tasks: BackgroundTasks):
@@ -113,7 +113,7 @@ async def execute_py_file(file_name: str,background_tasks: BackgroundTasks):
113
  # for file_name in file_list:
114
  # tasks.append(file_executer(file_name))
115
 
116
- # await asyncio.gather(*tasks)
117
 
118
  # if allow_submit:
119
  # cover_img = cover_image.file.read()
@@ -127,54 +127,28 @@ async def execute_py_file(file_name: str,background_tasks: BackgroundTasks):
127
 
128
 
129
 
130
- async def file_executer(file_name: str) -> "执行返回":
131
- try:
132
- print("开始执行py任务", file_name)
133
-
134
- output = []
135
- process = await asyncio.create_subprocess_exec("python", f"{file_name}.py", stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT)
136
-
137
- while True:
138
- line = await process.stdout.readline()
139
- if not line:
140
- break
141
- line_text = line.decode('utf-8').strip()
142
- print(line_text)
143
- output.append(line_text)
144
-
145
- await process.wait()
146
- result = '\n'.join(output)
147
-
148
- print("执行完成py任务:", file_name, "结果如下")
149
- print(result)
150
-
151
- return PlainTextResponse(result)
152
- except subprocess.CalledProcessError as e:
153
- print("执行py任务失败", file_name, "原因如下")
154
- print(f"Error executing {file_name}.py: {e}")
155
- return PlainTextResponse(f"Error executing {file_name}.py: {e}")
156
-
157
  @app.get("/execute_all_task")
158
  async def execute_all_task(background_tasks: BackgroundTasks, file_list: List[str] = Query(["1removeMask", "2magiDialogCut", "3mergeDialogToVideo"]),
159
  bili_meta_data: dict = None, cover_image: UploadFile = File(...), mp4_out_file: str = 'mp4_out/output.mp4',
160
  allow_submit: bool = False, cover_path: str = '/cover'
161
  ):
162
- print("获取到file_list是:", file_list)
 
 
 
 
163
  tasks = []
164
  for file_name in file_list:
165
- tasks.append(await file_executer(file_name))
 
 
 
 
 
 
166
 
167
- await asyncio.gather(*tasks)
168
 
169
- if allow_submit:
170
- cover_img = cover_image.file.read()
171
- cover_img_data = Image.open(io.BytesIO(cover_img)).convert("L").convert("RGB")
172
- cover_path_to_image = os.path.join(cover_path, f"cover.jpg")
173
- cover_img_data.save(cover_path_to_image)
174
- background_tasks.add_task(upload_video, bili_meta_data, cover_path_to_image)
175
- return {"message": "提交video任务已加入队列"}
176
 
177
- return {"message": "Tasks added to the queue"}
178
 
179
 
180
 
 
66
 
67
 
68
  ########异步处理py文件执行接口
69
+ def file_executer(file_name:str)->"执行返回":
70
+ try:
71
+ print("开始执行py任务",file_name)
72
+ # result = subprocess.check_output(["python", f"{file_name}.py"], stderr=subprocess.STDOUT).decode("utf-8")#执行完成后显示运行py的print
73
+ ##########test 边执行py边显示print+++++++++++++++++++++++++++++++++++++
74
+ # 开始执行Python 脚本
75
+ output = []
76
+ process = subprocess.Popen(["python", f"{file_name}.py"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
77
+ # 逐行读取脚本的输出并显示
78
+ for line in iter(process.stdout.readline, b''):
79
+ line_text = line.decode('utf-8').strip()
80
+ print(line_text)
81
+ output.append(line_text)
82
+ process.wait()
83
+ result = '\n'.join(output)
84
+ ##########test 边执行py边显示print+++++++++++++++++++++++++++++++++++++
85
 
86
+ print("执行完成py任务:",file_name,"结果如下")
87
+ print(result)
88
+ return PlainTextResponse(result)
89
+ except subprocess.CalledProcessError as e:
90
+ print("执行py任务失败",file_name,"原因如下")
91
+ print(f"Error executing {file_name}.py: {e}")
92
+ return PlainTextResponse(f"Error executing {file_name}.py: {e}")
93
 
94
  @app.get("/execute_py_file/{file_name}")
95
  async def execute_py_file(file_name: str,background_tasks: BackgroundTasks):
 
113
  # for file_name in file_list:
114
  # tasks.append(file_executer(file_name))
115
 
116
+ # # await asyncio.gather(*tasks) #尝试不用这个能否运行,老是这里报错
117
 
118
  # if allow_submit:
119
  # cover_img = cover_image.file.read()
 
127
 
128
 
129
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  @app.get("/execute_all_task")
131
  async def execute_all_task(background_tasks: BackgroundTasks, file_list: List[str] = Query(["1removeMask", "2magiDialogCut", "3mergeDialogToVideo"]),
132
  bili_meta_data: dict = None, cover_image: UploadFile = File(...), mp4_out_file: str = 'mp4_out/output.mp4',
133
  allow_submit: bool = False, cover_path: str = '/cover'
134
  ):
135
+
136
+ background_tasks.add_task(process_tasks_and_upload, file_list, bili_meta_data, cover_image, cover_path,background_tasks)
137
+ return {"message": "提交成功"}
138
+
139
+ def process_tasks_and_upload(file_list, bili_meta_data, cover_image, cover_path,background_tasks):
140
  tasks = []
141
  for file_name in file_list:
142
+ tasks.append(file_executer(file_name))
143
+
144
+ cover_img = cover_image.file.read()
145
+ cover_img_data = Image.open(io.BytesIO(cover_img)).convert("L").convert("RGB")
146
+ cover_path_to_image = os.path.join(cover_path, "cover.jpg")
147
+ cover_img_data.save(cover_path_to_image)
148
+ background_tasks.add_task(upload_video, bili_meta_data, cover_path_to_image)
149
 
 
150
 
 
 
 
 
 
 
 
151
 
 
152
 
153
 
154