rogerxavier commited on
Commit
b80bf3b
1 Parent(s): eb4aa47

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +57 -28
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):
@@ -102,17 +102,18 @@ async def execute_py_file(file_name: str,background_tasks: BackgroundTasks):
102
 
103
 
104
  #保证既要提交队列后返回给用户,又要先完成前面的video生成步骤再执行后面的submit函数,还有兼顾allow_submit的值
 
105
  # @app.get("/execute_all_task")
106
- # async def execute_all_task(background_tasks: BackgroundTasks, file_list: list = ["1removeMask", "2magiDialogCut", "3mergeDialogToVideo"],
107
  # bili_meta_data: dict = None, cover_image: UploadFile = File(...), mp4_out_file: str = 'mp4_out/output.mp4',
108
  # allow_submit: bool = False, cover_path: str = '/cover'
109
  # ):
110
- # print("获取到file_list是:",file_list)
111
  # tasks = []
112
  # for file_name in file_list:
113
  # tasks.append(file_executer(file_name))
114
 
115
- # await asyncio.gather(*tasks)#使用asyncio.gather(*tasks)来等待所有file_executer任务完成后再执行upload_video任务。
116
 
117
  # if allow_submit:
118
  # cover_img = cover_image.file.read()
@@ -124,6 +125,35 @@ async def execute_py_file(file_name: str,background_tasks: BackgroundTasks):
124
 
125
  # return {"message": "Tasks added to the queue"}
126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  @app.get("/execute_all_task")
128
  async def execute_all_task(background_tasks: BackgroundTasks, file_list: List[str] = Query(["1removeMask", "2magiDialogCut", "3mergeDialogToVideo"]),
129
  bili_meta_data: dict = None, cover_image: UploadFile = File(...), mp4_out_file: str = 'mp4_out/output.mp4',
@@ -132,7 +162,7 @@ async def execute_all_task(background_tasks: BackgroundTasks, file_list: List[st
132
  print("获取到file_list是:", file_list)
133
  tasks = []
134
  for file_name in file_list:
135
- tasks.append(file_executer(file_name))
136
 
137
  await asyncio.gather(*tasks)
138
 
@@ -152,7 +182,6 @@ async def execute_all_task(background_tasks: BackgroundTasks, file_list: List[st
152
 
153
 
154
 
155
-
156
  ##########异步样例
157
  def someTask():
158
  time.sleep(20)
 
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):
 
102
 
103
 
104
  #保证既要提交队列后返回给用户,又要先完成前面的video生成步骤再执行后面的submit函数,还有兼顾allow_submit的值
105
+
106
  # @app.get("/execute_all_task")
107
+ # async def execute_all_task(background_tasks: BackgroundTasks, file_list: List[str] = Query(["1removeMask", "2magiDialogCut", "3mergeDialogToVideo"]),
108
  # bili_meta_data: dict = None, cover_image: UploadFile = File(...), mp4_out_file: str = 'mp4_out/output.mp4',
109
  # allow_submit: bool = False, cover_path: str = '/cover'
110
  # ):
111
+ # print("获取到file_list是:", file_list)
112
  # tasks = []
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()
 
125
 
126
  # return {"message": "Tasks added to the queue"}
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',
 
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
 
 
182
 
183
 
184
 
 
185
  ##########异步样例
186
  def someTask():
187
  time.sleep(20)