網(wǎng)站的標(biāo)題與關(guān)鍵詞網(wǎng)絡(luò)營銷推廣的優(yōu)勢
在做AI模型推理的接口時,這時候接口是非異步的,但是uvicorn運(yùn)行FastAPI時就會出現(xiàn)阻塞所有請求。
這時候需要解決這個問題:
api.py:
import asyncio
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
import time
import io
import uvicornapp = FastAPI()def my_io(num):print(num)time.sleep(20)@app.get("/hello")
async def hello():loop = asyncio.get_event_loop()# my_io 里包含不支持異步操作的代碼, 所以就使用線程池來配合實(shí)現(xiàn)了。future = loop.run_in_executor(None , my_io , 666)response = await futureprint("運(yùn)行完成", response)return {"message" : "success"}def read_image_data(image_path : str):with open(image_path , "rb") as fr:datas = fr.read()return datas@app.get("/show_image/{image_path:path}")
async def show_image(image_path : str):datas = await asyncio.get_event_loop().run_in_executor(None , read_image_data , image_path)bytes = io.BytesIO(datas)return StreamingResponse(bytes , media_type="image/png")if __name__ == "__main__":uvicorn.run("api:app", host="0.0.0.0", port=10001, reload=True)
完美解決!!!perfect!!!?