rogerxavier commited on
Commit
2c03cb6
1 Parent(s): 33e8fe3

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +20 -4
api.py CHANGED
@@ -31,7 +31,7 @@ def t5():
31
 
32
 
33
 
34
-
35
  @app.post("/getOriginalMangaList")
36
  async def getOriginalMangaList(images: List[UploadFile] = File(...)):
37
  for idx, image in enumerate(images):
@@ -40,8 +40,9 @@ async def getOriginalMangaList(images: List[UploadFile] = File(...)):
40
  path_to_image = f"/manga/{idx}.jpg"
41
  image.save(path_to_image)
42
  return "获取图片保存成功"
 
43
 
44
-
45
  @app.delete("/deleteFiles")
46
  async def delete_files(directory: str):
47
  for filename in os.listdir(directory):
@@ -49,17 +50,31 @@ async def delete_files(directory: str):
49
  if os.path.isfile(file_path):
50
  os.remove(file_path)
51
  return {"message": f"成功删除{directory}目录下的所有文件"}
 
52
 
53
 
54
- @app.get("/execute_py_file/{file_name}")
55
- async def execute_py_file(file_name: str):
 
 
56
  try:
57
  result = subprocess.check_output(["python", f"{file_name}.py"]).decode("utf-8")
58
  return PlainTextResponse(result)
59
  except subprocess.CalledProcessError as e:
60
  return PlainTextResponse(f"Error executing {file_name}.py: {e}")
 
 
 
 
 
 
 
 
 
 
61
 
62
 
 
63
  def someTask():
64
  time.sleep(20)
65
  print("睡眠20s结束")
@@ -70,4 +85,5 @@ def returnRandomSubscribeUrl(background_tasks: BackgroundTasks)->str:
70
  result = "先返回"
71
  background_tasks.add_task(someTask)
72
  return result
 
73
 
 
31
 
32
 
33
 
34
+ ##这个比较快不用异步
35
  @app.post("/getOriginalMangaList")
36
  async def getOriginalMangaList(images: List[UploadFile] = File(...)):
37
  for idx, image in enumerate(images):
 
40
  path_to_image = f"/manga/{idx}.jpg"
41
  image.save(path_to_image)
42
  return "获取图片保存成功"
43
+ ##这个比较快不用异步
44
 
45
+ ##这个比较快不用异步
46
  @app.delete("/deleteFiles")
47
  async def delete_files(directory: str):
48
  for filename in os.listdir(directory):
 
50
  if os.path.isfile(file_path):
51
  os.remove(file_path)
52
  return {"message": f"成功删除{directory}目录下的所有文件"}
53
+ ##这个比较快不用异步
54
 
55
 
56
+
57
+
58
+ ########异步处理py文件执行接口
59
+ def file_executer(file_name:str)->"执行返回":
60
  try:
61
  result = subprocess.check_output(["python", f"{file_name}.py"]).decode("utf-8")
62
  return PlainTextResponse(result)
63
  except subprocess.CalledProcessError as e:
64
  return PlainTextResponse(f"Error executing {file_name}.py: {e}")
65
+
66
+ @app.get("/execute_py_file/{file_name}")
67
+ async def execute_py_file(file_name: str,background_tasks: BackgroundTasks):
68
+ result = "接受到了请求{filename}任务".format(filename = file_name)
69
+ background_tasks.add_task(file_executer,file_name)
70
+ return result
71
+ ########异步处理py文件执行接口
72
+
73
+
74
+
75
 
76
 
77
+ ##########异步样例
78
  def someTask():
79
  time.sleep(20)
80
  print("睡眠20s结束")
 
85
  result = "先返回"
86
  background_tasks.add_task(someTask)
87
  return result
88
+ ##########异步样例
89