81 lines
2.2 KiB
Python
Executable File
81 lines
2.2 KiB
Python
Executable File
"""
|
|
自动化回测服务 - 主入口
|
|
启动 FastAPI 服务,接受回测任务提交,执行回测,返回结果
|
|
遵循 vnpy 原生设计,只做外层封装
|
|
"""
|
|
import uvicorn
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from .config import settings
|
|
from .api import router
|
|
from .task_queue import task_queue
|
|
|
|
|
|
app = FastAPI(
|
|
title="sanguo 自动化回测服务",
|
|
description="基于 vnpy 原生 BacktestingEngine 的自动化回测API服务",
|
|
version="1.0.0"
|
|
)
|
|
|
|
# CORS 配置
|
|
if settings.cors_allow_all:
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# 注册API路由
|
|
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",
|
|
data=HealthCheckResponse(
|
|
pending_count=len(task_queue.pending_tasks),
|
|
running_count=len(task_queue.running_tasks),
|
|
completed_count=len(task_queue.completed_tasks),
|
|
failed_count=len(task_queue.failed_tasks),
|
|
max_workers=settings.max_workers
|
|
)
|
|
)
|
|
|
|
|
|
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()
|