Commit 6c1e1115 by 杨林龙

优化异步识别

1 parent 91b5ff34
Showing 1 changed file with 12 additions and 21 deletions
...@@ -16,6 +16,7 @@ from fastapi import Body, FastAPI, File, UploadFile ...@@ -16,6 +16,7 @@ from fastapi import Body, FastAPI, File, UploadFile
from funasr_service import FunasrService from funasr_service import FunasrService
from pydantic import BaseModel from pydantic import BaseModel
from funasr.download.file import download_from_url from funasr.download.file import download_from_url
import multiprocessing
logger = logging.getLogger() logger = logging.getLogger()
app = FastAPI() app = FastAPI()
...@@ -169,17 +170,16 @@ async def create_upload_file(file: UploadFile): ...@@ -169,17 +170,16 @@ async def create_upload_file(file: UploadFile):
return {"error": str(e)} return {"error": str(e)}
def url_to_byte_stream(url): def deal_worker(url):
# 发送HTTP请求获取文件内容 """
response = requests.get(url) 要执行的函数,在子进程中运行
"""
print(f"Worker {url} is running...")
# 解析音频
res = FunasrService(url).transform()
time.sleep(1)
print(f"Worker {url} finished.")
# 检查请求是否成功
if response.status_code == 200:
# 返回HTTP响应的内容
return response.content
else:
print(f"Failed to retrieve the file. Status code: {response.status_code}")
return None
class UrlParam(BaseModel): class UrlParam(BaseModel):
url: str url: str
...@@ -187,20 +187,11 @@ class UrlParam(BaseModel): ...@@ -187,20 +187,11 @@ class UrlParam(BaseModel):
@app.post("/funasr/url") @app.post("/funasr/url")
async def create_url(param: UrlParam): async def create_url(param: UrlParam):
try: try:
url = param.url process = multiprocessing.Process(target=deal_worker, args=(param.url))
print(f"url=={url}") process.start()
# file = download_from_url(url)
# print({f"file=={file}"})
asyncio.run(async_task(url))
return {"msg": "hello_world"}
except Exception as e: except Exception as e:
return {"error": str(e)} return {"error": str(e)}
async def async_task(url: str):
file_stream = url_to_byte_stream(url)
print("its ok")
asyncio.create_task(create_file(file_stream))
if __name__ == "__main__": if __name__ == "__main__":
save_path = "./audio/" save_path = "./audio/"
if os.path.exists(save_path): if os.path.exists(save_path):
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!