rogerxavier commited on
Commit
a3e21b5
1 Parent(s): 944b5f7

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +37 -10
api.py CHANGED
@@ -13,6 +13,8 @@ import subprocess
13
  from fastapi import BackgroundTasks
14
  import time
15
  from bilibili_api import sync, video_uploader, Credential#bili上传部分
 
 
16
 
17
  sessdata = os.getenv('sessdata')
18
  bili_jct = os.getenv('bili_jct')
@@ -100,30 +102,55 @@ async def execute_py_file(file_name: str,background_tasks: BackgroundTasks):
100
 
101
 
102
  #收到图片后为了后台任务的顺序,单独设定一个函数依次将自定义执行顺序加入队列 ,有默认参数的写后面-并且和bili合并,不然还要等output.mp4生成后才能发起提交请求
103
- @app.get("/execute_all_task")
104
- async def execute_all_task(background_tasks: BackgroundTasks,file_list: list = ["1removeMask", "2magiDialogCut", "3mergeDialogToVideo"],
105
- bili_meta_data:dict = None,cover_image: UploadFile = File(...),mp4_out_file:str='mp4_out/output.mp4',
106
- allow_submit:bool=False,cover_path:str ='/cover'
107
 
108
- ):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  for file_name in file_list:
110
- background_tasks.add_task(file_executer, file_name)
 
 
 
111
  if allow_submit:
112
- #如果请求中设置允许上传output.mp4
113
  cover_img = cover_image.file.read()
114
  cover_img_data = Image.open(io.BytesIO(cover_img)).convert("L").convert("RGB")
115
  cover_path_to_image = os.path.join(cover_path, f"cover.jpg")
116
  cover_img_data.save(cover_path_to_image)
117
- #video数据从meta用户请求获取,至于
118
- background_tasks.add_task(upload_video, bili_meta_data,cover_path_to_image)
119
  return {"message": "提交video任务已加入队列"}
120
-
121
  return {"message": "Tasks added to the queue"}
122
 
123
 
124
 
125
 
126
 
 
 
127
  ##########异步样例
128
  def someTask():
129
  time.sleep(20)
 
13
  from fastapi import BackgroundTasks
14
  import time
15
  from bilibili_api import sync, video_uploader, Credential#bili上传部分
16
+ import concurrent.futures #线程保证后台任务的先后
17
+
18
 
19
  sessdata = os.getenv('sessdata')
20
  bili_jct = os.getenv('bili_jct')
 
102
 
103
 
104
  #收到图片后为了后台任务的顺序,单独设定一个函数依次将自定义执行顺序加入队列 ,有默认参数的写后面-并且和bili合并,不然还要等output.mp4生成后才能发起提交请求
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
+ # ):
111
+ # for file_name in file_list:
112
+ # background_tasks.add_task(file_executer, file_name)
113
+ # if allow_submit:
114
+ # #如果请求中设置允许上传output.mp4
115
+ # cover_img = cover_image.file.read()
116
+ # cover_img_data = Image.open(io.BytesIO(cover_img)).convert("L").convert("RGB")
117
+ # cover_path_to_image = os.path.join(cover_path, f"cover.jpg")
118
+ # cover_img_data.save(cover_path_to_image)
119
+ # #video数据从meta用户请求获取,至于
120
+ # background_tasks.add_task(upload_video, bili_meta_data,cover_path_to_image)
121
+ # return {"message": "提交video任务已加入队列"}
122
+
123
+ # return {"message": "Tasks added to the queue"}
124
+
125
+
126
+ #保证既要提交队列后返回给用户,又要先完成前面的video生成步骤再执行后面的submit函数,还有兼顾allow_submit的值
127
+ @app.get("/execute_all_task")
128
+ async def execute_all_task(background_tasks: BackgroundTasks, file_list: list = ["1removeMask", "2magiDialogCut", "3mergeDialogToVideo"],
129
+ bili_meta_data: dict = None, cover_image: UploadFile = File(...), mp4_out_file: str = 'mp4_out/output.mp4',
130
+ allow_submit: bool = False, cover_path: str = '/cover'
131
+ ):
132
+ tasks = []
133
  for file_name in file_list:
134
+ tasks.append(file_executer(file_name))
135
+
136
+ await asyncio.gather(*tasks)#使用asyncio.gather(*tasks)来等待所有file_executer任务完成后再执行upload_video任务。
137
+
138
  if allow_submit:
 
139
  cover_img = cover_image.file.read()
140
  cover_img_data = Image.open(io.BytesIO(cover_img)).convert("L").convert("RGB")
141
  cover_path_to_image = os.path.join(cover_path, f"cover.jpg")
142
  cover_img_data.save(cover_path_to_image)
143
+ background_tasks.add_task(upload_video, bili_meta_data, cover_path_to_image)
 
144
  return {"message": "提交video任务已加入队列"}
145
+
146
  return {"message": "Tasks added to the queue"}
147
 
148
 
149
 
150
 
151
 
152
+
153
+
154
  ##########异步样例
155
  def someTask():
156
  time.sleep(20)