auto-sync: 2026-04-28 23:16:42
This commit is contained in:
@@ -4,18 +4,33 @@
|
||||
遵循 vnpy 原生设计,只做外层封装
|
||||
"""
|
||||
import uvicorn
|
||||
from contextlib import asynccontextmanager
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from .config import settings
|
||||
from .api import router
|
||||
from .task_queue import task_queue
|
||||
from .models import ApiResponse, HealthCheckResponse
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
"""应用生命周期:启动时开启worker线程,关闭时停止"""
|
||||
# 启动
|
||||
task_queue.start_worker_pool()
|
||||
print(f"✅ 回测服务启动 (max_workers={settings.max_workers})")
|
||||
yield
|
||||
# 关闭
|
||||
task_queue.close_worker_pool()
|
||||
print("回测服务已停止")
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
title="sanguo 自动化回测服务",
|
||||
description="基于 vnpy 原生 BacktestingEngine 的自动化回测API服务",
|
||||
version="1.0.0"
|
||||
version="1.0.0",
|
||||
lifespan=lifespan
|
||||
)
|
||||
|
||||
# CORS 配置
|
||||
@@ -32,15 +47,9 @@ if settings.cors_allow_all:
|
||||
app.include_router(router, tags=["backtest"])
|
||||
|
||||
|
||||
# 健康检查已经在API路由中定义
|
||||
from .models import ApiResponse, HealthCheckResponse
|
||||
from .task_queue import task_queue
|
||||
|
||||
|
||||
@app.get("/api/backtest/health", summary="服务健康检查", response_model=ApiResponse[HealthCheckResponse])
|
||||
def health():
|
||||
"""服务健康检查,返回当前任务统计"""
|
||||
from .models import ApiResponse, HealthCheckResponse
|
||||
return ApiResponse(
|
||||
code=0,
|
||||
msg="ok",
|
||||
@@ -54,27 +63,10 @@ def health():
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
"""启动服务"""
|
||||
# 启动工作进程池
|
||||
task_queue.start_worker_pool()
|
||||
|
||||
print(f"自动化回测服务启动中...")
|
||||
print(f"监听地址: http://{settings.host}:{settings.port}")
|
||||
print(f"最大并发回测数: {settings.max_workers}")
|
||||
print(f"API 文档: http://{settings.host}:{settings.port}/docs")
|
||||
|
||||
try:
|
||||
uvicorn.run(
|
||||
"backtest_service.main:app",
|
||||
host=settings.host,
|
||||
port=settings.port,
|
||||
reload=settings.debug,
|
||||
)
|
||||
finally:
|
||||
# 关闭进程池
|
||||
task_queue.close_worker_pool()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
uvicorn.run(
|
||||
"backtest_service.main:app",
|
||||
host=settings.host,
|
||||
port=settings.port,
|
||||
reload=settings.debug,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user