Refine
This commit is contained in:
24
main.py
24
main.py
@@ -33,6 +33,8 @@ if not os.path.exists(inpDir):
|
|||||||
if not os.path.exists(tmpDir):
|
if not os.path.exists(tmpDir):
|
||||||
os.mkdir(tmpDir)
|
os.mkdir(tmpDir)
|
||||||
|
|
||||||
|
lock_simulation = asyncio.Lock()
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
||||||
@@ -146,19 +148,21 @@ async def fastapi_dump_inp(network: str, inp: str) -> bool:
|
|||||||
dump_inp(network, inp)
|
dump_inp(network, inp)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
app.state.is_simulation = False
|
|
||||||
|
|
||||||
# 必须用这个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 app.state.is_simulation:
|
|
||||||
raise HTTPException(status_code=409, detail="is in simulation")
|
|
||||||
else:
|
|
||||||
app.state.is_simulation = True
|
|
||||||
result = run_project(network)
|
|
||||||
app.state.is_simulation = False
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
if lock_simulation.locked():
|
||||||
|
raise HTTPException(status_code=409, detail="is in simulation")
|
||||||
|
|
||||||
|
await lock_simulation.acquire()
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = run_project(network)
|
||||||
|
return result
|
||||||
|
finally:
|
||||||
|
lock_simulation.release() # Release the lock
|
||||||
|
|
||||||
# put in inp folder, name without extension
|
# put in inp folder, name without extension
|
||||||
@app.get("/runinp/")
|
@app.get("/runinp/")
|
||||||
async def fastapi_run_inp(network: str) -> str:
|
async def fastapi_run_inp(network: str) -> str:
|
||||||
|
|||||||
Reference in New Issue
Block a user