1 Star 0 Fork 0

天钰/tyai-server

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.py 1.55 KB
一键复制 编辑 原始数据 按行查看 历史
timfeng 提交于 2024-07-04 23:40 . update
import time
import uvicorn as uvicorn
from fastapi import FastAPI
from config.celery_utils import create_celery
from routers import universities,sherpa
import subprocess
from multiprocessing import Process
import config
def create_app() -> FastAPI:
current_app = FastAPI(title="Asynchronous tasks processing with Celery and RabbitMQ",
description="Sample FastAPI Application to demonstrate Event "
"driven architecture with Celery and RabbitMQ",
version="1.0.0", )
current_app.celery_app = create_celery()
current_app.include_router(universities.router)
current_app.include_router(sherpa.router)
return current_app
app = create_app()
celery = app.celery_app
@app.middleware("http")
async def add_process_time_header(request, call_next):
print('inside middleware!')
start_time = time.time()
response = await call_next(request)
process_time = time.time() - start_time
response.headers["X-Process-Time"] = str(f'{process_time:0.4f} sec')
return response
def start_celery():
subprocess.run(['celery', '-A', 'main.celery', 'worker', '-l', 'info','-Q',config.get_queues(),'-c','4'])
def start_celery_flower():
subprocess.run(['celery', '-A', 'main.celery', 'flower', '--port=5555'])
if __name__ == "__main__":
celery_process = Process(target=start_celery)
celery_process.start()
celery_flower_process = Process(target=start_celery_flower)
celery_flower_process.start()
uvicorn.run("main:app", port=9000, reload=True)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/gdty/tyai-server.git
git@gitee.com:gdty/tyai-server.git
gdty
tyai-server
tyai-server
master

搜索帮助