parent
69e1cd7b6c
commit
cc444966f0
@ -1,46 +1,173 @@ |
|||||||
# -*- coding: utf-8 -*- |
# -*- coding: utf-8 -*- |
||||||
"""主程序入口""" |
"""主程序入口 - FastAPI版本""" |
||||||
|
|
||||||
import time |
import time |
||||||
|
from fastapi import FastAPI, HTTPException |
||||||
from core.simulator import AlphaSimulator |
from core.simulator import AlphaSimulator |
||||||
from config.settings import settings |
|
||||||
from utils.helpers import retry_on_exception |
|
||||||
|
|
||||||
|
# 创建FastAPI应用 |
||||||
|
app = FastAPI( |
||||||
|
title="Alpha因子模拟器API", |
||||||
|
description="通过HTTP接口触发的因子模拟器", |
||||||
|
version="1.0.0" |
||||||
|
) |
||||||
|
|
||||||
|
# 全局变量 |
||||||
|
_simulator = None |
||||||
|
_last_result = None # 存储上次执行结果 |
||||||
|
_is_processing = False # 是否正在处理 |
||||||
|
|
||||||
|
|
||||||
|
def initialize_simulator(): |
||||||
|
"""初始化模拟器""" |
||||||
|
global _simulator |
||||||
|
if _simulator is None: |
||||||
|
_simulator = AlphaSimulator() |
||||||
|
_simulator.initialize() |
||||||
|
print("模拟器初始化完成") |
||||||
|
return _simulator |
||||||
|
|
||||||
@retry_on_exception(max_retries=3, delay=5.0) |
|
||||||
def main_loop(simulator: AlphaSimulator) -> None: |
|
||||||
"""主循环""" |
|
||||||
if simulator.needs_token_refresh(): |
|
||||||
print("Token需要刷新,重新登录...") |
|
||||||
simulator.login() |
|
||||||
|
|
||||||
alpha_list = simulator.load_alpha_list() |
@app.get("/") |
||||||
if not alpha_list: |
async def root(): |
||||||
print("暂无待处理的alpha表达式,10分钟后重新检查...") |
"""根路径,返回服务信息""" |
||||||
time.sleep(600) |
return { |
||||||
return |
"service": "Alpha因子模拟器", |
||||||
|
"version": "1.0.0", |
||||||
|
"status": "running", |
||||||
|
"endpoints": { |
||||||
|
"run_simulation": "POST /run - 触发一次模拟执行", |
||||||
|
"last_result": "GET /result - 获取上次执行结果", |
||||||
|
"status": "GET /status - 查看服务状态", |
||||||
|
"health": "GET /health - 健康检查" |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
print(f"共加载 {len(alpha_list)} 个需要模拟的因子表达式") |
|
||||||
success_count, fail_count = simulator.run_batch_simulation(alpha_list) |
|
||||||
print(f"本轮处理完成: 成功 {success_count} 个, 失败 {fail_count} 个") |
|
||||||
|
|
||||||
|
@app.post("/run") |
||||||
|
async def run_simulation(): |
||||||
|
""" |
||||||
|
触发一次模拟执行 |
||||||
|
执行顺序: |
||||||
|
1. 刷新token |
||||||
|
2. 加载alpha列表 |
||||||
|
3. 执行批量模拟 |
||||||
|
""" |
||||||
|
global _is_processing, _last_result |
||||||
|
|
||||||
def main(): |
# 检查是否正在处理 |
||||||
"""主函数""" |
if _is_processing: |
||||||
simulator = AlphaSimulator() |
return { |
||||||
simulator.initialize() |
"status": "busy", |
||||||
|
"message": "已有模拟任务正在执行中,请稍后再试", |
||||||
|
"timestamp": time.time() |
||||||
|
} |
||||||
|
|
||||||
try: |
try: |
||||||
while True: |
_is_processing = True |
||||||
main_loop(simulator) |
|
||||||
print(f"等待{settings.CHECK_INTERVAL // 60}分钟后继续检查...") |
# 初始化模拟器 |
||||||
time.sleep(settings.CHECK_INTERVAL) |
simulator = initialize_simulator() |
||||||
except KeyboardInterrupt: |
|
||||||
print("\n程序被用户中断") |
# 1. 刷新token |
||||||
|
print("刷新token...") |
||||||
|
simulator.login() |
||||||
|
|
||||||
|
# 2. 加载alpha列表 |
||||||
|
print("加载alpha列表...") |
||||||
|
alpha_list = simulator.load_alpha_list() |
||||||
|
|
||||||
|
if not alpha_list: |
||||||
|
result = { |
||||||
|
"status": "no_data", |
||||||
|
"message": "数据库中没有待处理的alpha表达式", |
||||||
|
"timestamp": time.time() |
||||||
|
} |
||||||
|
_last_result = result |
||||||
|
return result |
||||||
|
|
||||||
|
# 3. 执行批量模拟 |
||||||
|
print(f"共加载 {len(alpha_list)} 个需要模拟的因子表达式") |
||||||
|
success_count, fail_count = simulator.run_batch_simulation(alpha_list) |
||||||
|
|
||||||
|
result_message = f"处理完成: 成功 {success_count} 个, 失败 {fail_count} 个" |
||||||
|
print(result_message) |
||||||
|
|
||||||
|
result = { |
||||||
|
"status": "completed", |
||||||
|
"message": result_message, |
||||||
|
"data": { |
||||||
|
"alpha_count": len(alpha_list), |
||||||
|
"success_count": success_count, |
||||||
|
"fail_count": fail_count |
||||||
|
}, |
||||||
|
"timestamp": time.time() |
||||||
|
} |
||||||
|
|
||||||
|
_last_result = result |
||||||
|
return result |
||||||
|
|
||||||
except Exception as e: |
except Exception as e: |
||||||
print(f"程序执行异常: {e}") |
error_msg = f"模拟执行失败: {str(e)}" |
||||||
raise |
print(error_msg) |
||||||
|
|
||||||
|
result = { |
||||||
|
"status": "error", |
||||||
|
"message": error_msg, |
||||||
|
"timestamp": time.time() |
||||||
|
} |
||||||
|
_last_result = result |
||||||
|
return result |
||||||
|
|
||||||
|
finally: |
||||||
|
_is_processing = False |
||||||
|
|
||||||
|
|
||||||
|
@app.get("/result") |
||||||
|
async def get_last_result(): |
||||||
|
"""获取上次执行的结果""" |
||||||
|
if _last_result is None: |
||||||
|
raise HTTPException(status_code=404, detail="暂无执行结果") |
||||||
|
|
||||||
|
return { |
||||||
|
"status": "success", |
||||||
|
"data": _last_result, |
||||||
|
"timestamp": time.time() |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@app.get("/status") |
||||||
|
async def get_status(): |
||||||
|
"""获取服务状态""" |
||||||
|
return { |
||||||
|
"is_processing": _is_processing, |
||||||
|
"simulator_initialized": _simulator is not None, |
||||||
|
"has_last_result": _last_result is not None, |
||||||
|
"timestamp": time.time() |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@app.get("/health") |
||||||
|
async def health_check(): |
||||||
|
"""健康检查端点""" |
||||||
|
return { |
||||||
|
"status": "healthy", |
||||||
|
"timestamp": time.time(), |
||||||
|
"simulator_initialized": _simulator is not None |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__": |
if __name__ == "__main__": |
||||||
main() |
import uvicorn |
||||||
|
|
||||||
|
# 首次初始化 |
||||||
|
initialize_simulator() |
||||||
|
print("服务正在启动...") |
||||||
|
|
||||||
|
# 启动FastAPI服务 |
||||||
|
uvicorn.run( |
||||||
|
app, |
||||||
|
host="0.0.0.0", |
||||||
|
port=8000, |
||||||
|
log_level="info" |
||||||
|
) |
||||||
Loading…
Reference in new issue