| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- import uvicorn
- import logging
- from fastapi import FastAPI, BackgroundTasks, Request
- from contextlib import asynccontextmanager
- from pydantic import BaseModel
- from typing import List
- import threading
- import time
- from conf.settings import logger,HOST,PORT
- from task import Thread_que,run_task,detect_list
- @asynccontextmanager
- async def lifespan(app: FastAPI):
- # 在应用启动前运行的函数
- thread = threading.Thread(target=run_task)
- thread.start()
- yield
- # 在应用关闭后运行的函数
- Thread_que.put_nowait("stop")
- thread.join()
- logger.info("Shutting down...")
- logger.info("FastAPI docs: ")
- app = FastAPI(lifespan=lifespan)
- class Item(BaseModel):
- str: str
- @app.get("/")
- async def list_detect_list():
- return detect_list.list
- @app.post("/")
- async def add_live(request: Request):
- json_body = await request.json()
- Thread_que.put_nowait({"add":json_body})
- # 你的代码
- return {"status": "ok"}
- def main():
- uvicorn.run(app, host=HOST, port=PORT, log_level="info")
- if __name__ == "__main__":
- main()
|