Add fastapi fastapi_run_project_return_dict to return the text version of simulation results

This commit is contained in:
DingZQ
2025-02-05 15:06:00 +08:00
parent 1996ffd17d
commit 5f9b5bd310
4 changed files with 65 additions and 4 deletions

29
main.py
View File

@@ -207,11 +207,34 @@ async def fastapi_run_project(network: str) -> str:
raise HTTPException(status_code=409, detail="is in simulation")
else:
try:
result = run_project(network)
return result
return run_project(network)
finally:
# 手动释放锁(可选,依赖过期时间自动释放更安全)
redis_client.delete(lock_key)
# DingZQ, 2025-02-04, 返回dict[str, Any]
# output 和 report
# output 是 json
# report 是 text
@app.get("/runprojectreturndict/")
async def fastapi_run_project_return_dict(network: str) -> dict[str, Any]:
lock_key = "exclusive_api_lock"
timeout = 120 # 锁自动过期时间(秒)
# 尝试获取锁NX=True: 不存在时设置EX=timeout: 过期时间)
acquired = redis_client.set(lock_key, "locked", nx=True, ex=timeout)
logger.info(f"acquired : {acquired}")
if not acquired:
raise HTTPException(status_code=409, detail="is in simulation")
else:
try:
return run_project_return_dict(network)
finally:
# 手动释放锁(可选,依赖过期时间自动释放更安全)
redis_client.delete(lock_key)
# put in inp folder, name without extension
@app.get("/runinp/")
@@ -1897,7 +1920,7 @@ async def upload_inp(afile: bytes, name: str ):
return True
@app.get("/downloadinp/", status_code=status.HTTP_200_OK)
async def download_inp(name: str, response: Response):
async def download_name: str, response: Response):
filePath = inpDir + name
if os.path.exists(filePath):
return FileResponse(filePath, media_type='application/octet-stream', filename="inp.inp")