Refine
This commit is contained in:
20
main.py
20
main.py
@@ -11,6 +11,7 @@ from fastapi import FastAPI, File, UploadFile, Response, status, Request, Body,
|
|||||||
from fastapi.responses import PlainTextResponse
|
from fastapi.responses import PlainTextResponse
|
||||||
from fastapi.middleware.gzip import GZipMiddleware
|
from fastapi.middleware.gzip import GZipMiddleware
|
||||||
from tjnetwork import *
|
from tjnetwork import *
|
||||||
|
import asyncio
|
||||||
|
|
||||||
JUNCTION = 0
|
JUNCTION = 0
|
||||||
RESERVOIR = 1
|
RESERVOIR = 1
|
||||||
@@ -32,7 +33,8 @@ if not os.path.exists(tmpDir):
|
|||||||
os.mkdir(tmpDir)
|
os.mkdir(tmpDir)
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
app.state.is_simulation = False
|
|
||||||
|
lock = asyncio.Lock()
|
||||||
|
|
||||||
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
||||||
|
|
||||||
@@ -147,14 +149,16 @@ async def fastapi_dump_inp(network: str, inp: str) -> bool:
|
|||||||
|
|
||||||
# 必须用这个PlainTextResponse,不然每个key都有引号
|
# 必须用这个PlainTextResponse,不然每个key都有引号
|
||||||
@app.get("/runproject/", response_class = PlainTextResponse)
|
@app.get("/runproject/", response_class = PlainTextResponse)
|
||||||
def fastapi_run_project(network: str) -> str:
|
async def fastapi_run_project(network: str) -> str:
|
||||||
if not app.state.is_simulation:
|
if lock.locked():
|
||||||
app.state.is_simulation = True
|
raise HTTPException(status_code=400, detail="is in simulation")
|
||||||
result = run_project(network)
|
|
||||||
app.state.is_simulation = False
|
|
||||||
return result
|
|
||||||
else:
|
else:
|
||||||
raise HTTPException(status_code=400, detail="Is in Simulation")
|
try:
|
||||||
|
async with lock:
|
||||||
|
result = run_project(network)
|
||||||
|
return result
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
raise HTTPException(status_code=400, detail="is in simulation")
|
||||||
|
|
||||||
# put in inp folder, name without extension
|
# put in inp folder, name without extension
|
||||||
@app.get("/runinp/")
|
@app.get("/runinp/")
|
||||||
|
|||||||
Reference in New Issue
Block a user